springboot整合redis实践消息发布订阅特性!!!

需求描述

springboot框架整合redis实现发布订阅消息模式,将消息发布到redis一个主题topic,当有订阅到这个topic会接收到这个消息并进行消费。

创建redis监听器管理者

注册订阅代码如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.Topic;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@Configuration
public class RedisMessageListenerHolder {
    @Autowired    
    private RedisTemplate redisTemplate = null;
    // Redis 连接工厂    
    @Autowired    
    private RedisConnectionFactory connectionFactory = null;
    // Redis 消息监听器    
    @Autowired    
    private MessageListener redisMsgListener = null;
      // 任务池    
    private ThreadPoolTaskScheduler taskScheduler = null;
    /**     
      * 创建任务池,运行线程等待处理 Redis 的消息     
      * */    
    @Bean    
    public ThreadPoolTaskScheduler initTaskScheduler() {        
    if (taskScheduler != null) {           
     return taskScheduler;        }      
       taskScheduler = new ThreadPoolTaskScheduler();       
        taskScheduler.setPoolSize(20);    
            return taskScheduler;    
            }
/**   
  * 定义 Redis 的监听容器     
  * */    
 @Bean    
 public RedisMessageListenerContainer initRedisContainer() {        
 RedisMessageListenerContainer container = new RedisMessageListenerContainer();       
 // Redis 连接工厂        
 container.setConnectionFactory(connectionFactory);       
  // 设置运行任务池        
  container.setTaskExecutor(initTaskScheduler());        
  // 定义监听渠道,名称为 topic1       
   Topic topic = new ChannelTopic("topic1");        
   // 使用监听器监听 Redis 的消息        
   container.addMessageListener(redisMsgListener, topic);        
   return container;   
    }
    }

监听器管理者的代码解析
监听器消费代码如下:

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
@Componentpublic class RedisMessageSubscriber implements MessageListener {
    @Override    
    public void onMessage(Message message, byte[] pattern) {        
    System.out.println("[监听到redis消息]");        
    // 消息体        
    String body = new String(message.getBody());        
    // 渠道名称        
    String topic = new String(pattern);        
    System.out.println(body);        
    System.out.println(topic);    
    }
    }

监听器消费代码解析
1、首先实现MessageListener接口,这个接口是spring-data-redis整合包。
2、实现onMessage方法,当有订阅到topic消息,会接收到Message消息。
3、代码方法可以对消息进行消费处理。

推送消息测试

推送消息代码如下,编写个controller接口,向redis的topic1订阅主题推送消息:

@RestController@RequestMapping("/mq")
public class RedisMessageController {    
@Autowired    
private RedisTemplate redisTemplate;
    @GetMapping("/publish")    
    public ResponseEntity publish(String msg) {        
    redisTemplate.convertAndSend("topic1", msg);        
    return ResponseEntity.ok("success");    
    }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值