1、Redis 配置文件:
public class RedisConfig { @Value("${redis.host}") private String host; @Value("${redis.port}") private int port; @Value("${redis.password}") private String pwd; @Bean public JedisPool jedisPoolFactory() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(100); jedisPoolConfig.setMaxWaitMillis(10000); // 连接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认true jedisPoolConfig.setBlockWhenExhausted(false); // 是否启用pool的jmx管理功能, 默认true jedisPoolConfig.setJmxEnabled(true); JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, 10000, StringUtils.isEmpty(rdps) ? null : rdps); return jedisPool; } @Bean(value = "redisTemplate") @Primary public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); // 设置value的序列化规则和 key的序列化规则 redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean("valueOperations") public ValueOperations<String, Object> valueOperations( @Autowired RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForValue(); } @Bean public HashOperations<String, Object, Object> hashOperations( @Autowired RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForHash(); } @Bean public SetOperations<String, Object> setOperations( @Autowired RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForSet(); } @Bean public ZSetOperations<String, Object> zSetOperations( @Autowired RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); } @Bean public ListOperations<String, Object> listOperations( @Autowired RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForList(); } /** * Redis消息监听器容器 * 这个容器加载了RedisConnectionFactory和消息监听器 * 可以添加多个监听不同话题的redis监听器,只需要把消息监听器和相应的消息订阅处理器绑定,该消息监听器 * 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理 * * @param connectionFactory 链接工厂 * @param adapter 适配器 * @return redis消息监听容器 */ @Bean public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter adapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); //可以添加多个 messageListener Map<String, TopicListener> messageSubscribers = SpringUtil.getBeansOfType(TopicListener.class); if (messageSubscribers != null && !messageSubscribers.isEmpty()) { for (Map.Entry<String, TopicListener> entry : messageSubscribers.entrySet()) { TopicListener messageSubscriber = entry.getValue(); container.addMessageListener(new MessageListenerAdapter(messageSubscriber), messageSubscriber.getTopic()); } } return container; } /** * 消息监听器适配器,绑定消息处理器,利用反射技术调用消息处理器的业务方法 * 将MessageReceiver注册为一个消息监听器,可以自定义消息接收的方法(handleMessage) * 如果不指定消息接收的方法,消息监听器会默认的寻找MessageReceiver中的onMessage这个方法作为消息接收的方法 * * @param messageReceiver 消息接受 * @return 适配器 */ @Bean public MessageListenerAdapter adapter(DataDicCache messageReceiver) { return new MessageListenerAdapter(messageReceiver); } } --------------------------------------------------------------------------------------
2、编写一个 redis 发布订阅自定义接口 TopicListener 继承MessageListener
public interface TopicListener extends MessageListener { Topic getTopic(); }
1> 消费者:实现 getTopic方法,指定那个管道
@Override public Topic getTopic() { return new ChannelTopic(TASK_CHANNEL_TOPIC); }
@Override public void onMessage(Message message, byte[] pattern) { log.info("分析接收通知); //TOTO 想要发送的代码逻辑 }
2> 生产者:管道消费者和发布者相同
redisTemplate.convertAndSend(TASK_CHANNEL_TOPIC, TmpConstant.TASK_RESATRJOB_MESSAGE);
convertAndSend:发布消息