我这边的应用场景是接收对方定时推送的消息,首先得双方沟通好消息的渠道,比如的channeid就是roomcontroller
package com.jeesite.modules.common.web;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeesite.modules.common.service.FastJson2JsonRedisSerializer;
import com.jeesite.modules.smartRoom.controller.RoomDeviceListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis配置
*
* @author lcy
*/
@Configuration
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport {
@Autowired
private RedisConnectionFactory factory;
@Autowired
private RoomDeviceListener redisDeviceListener;
/**
* 后台topic
*/
@Value("${spring.redis.roomControl:roomControl}")
private String roomControl;
@Bean(name = "server")
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//设置序列化Value的实例化对象
redisTemplate.setValueSerializer(jacksonSerializer());
redisTemplate.setHashValueSerializer(jacksonSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
@Bean(name = "message")
public RedisTemplate<String, String> redisTemplate2() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(factory);
return redisTemplate;
}
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
@Bean
public FastJson2JsonRedisSerializer jacksonSerializer() {
FastJson2JsonRedisSerializer fastJson2JsonRedisSerializer = new FastJson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
fastJson2JsonRedisSerializer.setObjectMapper(objectMapper);
return fastJson2JsonRedisSerializer;
}
/**
* 创建监听工厂,这里可以进行多个渠道的监听
*
* @return
*/
@Bean
public RedisMessageListenerContainer container() {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(factory);
//监听的主题
container.addMessageListener(roomControllerMessageAdapter(),new PatternTopic(roomControl) );
return container;
}
@Bean
public MessageListenerAdapter roomControllerMessageAdapter() {
MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(redisDeviceListener, "roomControlMessage");
return messageListenerAdapter;
}
}
在这里插入代码片
@Slf4j
@Component
public class RoomDeviceListener {
private final String[] focusDeviceType={"0200","1400","1900"};
private final List<String> focusDeviceTypeList= Arrays.asList(focusDeviceType);
/**
* 接收设备初始数据消息
*
* @param message
*/
public void roomControlMessage(String message) {
//监听到的消息message
System.out.println(message);
}
}