zoukankan      html  css  js  c++  java
  • Redis实现消息的发布/订阅

    利用spring-boot结合redis进行消息的发布与订阅:

      发布:

      class Publish {

        private static String topicName = “Topic:chat”;

        @Bean

        RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter messageListenerAdapter ) {

          RedisMessageListenerContainer  redisMessageListenerContainer  = new RedisMessageListenerContainer ();

          redisMessageListenerContainer.setConnectionFactory(connectionFactory);

          redisMessageListenerContainer.addMessageListenerAdapter(messageListenerAdapter ,new PatternTopic(topicName ));

          return  redisMessageListenerContainer;

        }

        @Bean

        MessageListenerAdapter messageListenerAdapter(Receiver recevier){

          return new MessageListenerAdapter (recevier,"receiveMessage");

        }

        @Bean

        Receiver recevier(@Value("Receiver-1") String name) {

          return new Receiver(name);

        }

        public static main(String[] args) {

          ApplicationContext ctx = SpringApplication.run(Publish .class,args);

          StringRedisTemple stringRedisTemple = ctx.getBean(StringRedisTemple.class);

          stringRedisTemple.convertAndSend(topicName ,"HAHAHAHAHAHA"); 

        }

      } 

          订阅:

      class Receiver {

        private String name;

        @Autowired

        public Receiver(String name) {

          this.name = name;

        }

        public void receiveMessage(String message) {

          System.ount.println(name + message);

        }

      }

      

  • 相关阅读:
    笔记-[5]-关于JS事件流!
    JS中的模块化开发之Sea.JS
    利用JavaScript如何创建一个table表格[第2种方法]
    二维数组左滑删除某一行
    sdwebimage缓存图片
    scrollView中内部控件的悬停
    (转)将图片保存到相册
    离屏渲染
    标签栏隐藏
    iOS8新特性之交互式通知
  • 原文地址:https://www.cnblogs.com/stanljj/p/7119687.html
Copyright © 2011-2022 走看看