栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【案例】SpringBoot使用Redis发布订阅

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【案例】SpringBoot使用Redis发布订阅

【案例】SpringBoot使用Redis发布订阅 第一种方式
  1. 步骤

    1. 配置监听消息类
    2. 添加监听容器
    3. 订阅频道
    4. 向频道发布消息
  2. 消息监听

    1. @Component
      public class RedisListener implements MessageListener {
      
          @Resource
          private RedisTemplate redisTemplate;
          @Override
          public void onMessage(Message message, byte[] pattern) {
      
              // 获取消息
              byte[] messageBody = message.getBody();
              // 使用值序列化器转换
              Object msg = redisTemplate.getValueSerializer().deserialize(messageBody);
              // 获取监听的频道
              byte[] channelByte = message.getChannel();
              // 使用字符串序列化器转换
              Object channel = redisTemplate.getStringSerializer().deserialize(channelByte);
              // 渠道名称转换
              String patternStr = new String(pattern);
              System.out.println(patternStr);
              System.out.println("---频道---: " + channel);
              System.out.println("---消息内容---: " + msg);
          }
      }
      
  3. 创建监听配置类

    1. 订阅多个频道使用通配符 *

    2. package com.mucd.config;
      
      import com.mucd.service.RedisListener;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.data.redis.connection.RedisConnectionFactory;
      import org.springframework.data.redis.listener.PatternTopic;
      import org.springframework.data.redis.listener.RedisMessageListenerContainer;
      
      
      @Configuration
      public class RedisConfiguration {
      
          @Bean
          public RedisMessageListenerContainer container(RedisConnectionFactory factory, RedisListener listener) {
              RedisMessageListenerContainer container = new RedisMessageListenerContainer();
              container.setConnectionFactory(factory);
              //订阅频道redis.news 和 redis.life  这个container 可以添加多个 messageListener
              container.addMessageListener(listener, patternTopic());
              return container;
          }
      
          
          @Bean
          public PatternTopic patternTopic(){
              return new PatternTopic("redis.*");
          }
      
      }
      
      
    3. 如果只订阅个别频道使用下面的方式

    4. package com.mucd.config;
      
      import com.mucd.service.RedisListener;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      import org.springframework.data.redis.connection.RedisConnectionFactory;
      import org.springframework.data.redis.listener.PatternTopic;
      import org.springframework.data.redis.listener.RedisMessageListenerContainer;
      
      
      @Configuration
      public class RedisConfiguration {
      
          @Bean
          public RedisMessageListenerContainer container(RedisConnectionFactory factory, RedisListener listener) {
              RedisMessageListenerContainer container = new RedisMessageListenerContainer();
              container.setConnectionFactory(factory);
              //container可以添加多个订阅频道
              container.addMessageListener(listener,new ChannelTopic("topic"));
              container.addMessageListener(listener,new ChannelTopic("topic02"));
              return container;
          }
      }
      
      
  4. 测试

    1. @Resource
      private RedisTemplate redisTemplate;
      
      @RequestMapping("/send")
      public void test01() {
          redisTemplate.convertAndSend("redis.username", "redis发布的第一条消息");
          redisTemplate.convertAndSend("redis.life", "redis发布的第一条消息");
          log.info("执行完成!");
      }
      

2022/07/21

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1024474.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号