• Redis消息订阅与发布


    监听器的创建

    package com.sogou.baike.testimport.testSubscribe;  
      
    import redis.clients.jedis.JedisPubSub;  
      
    
    public class RedisMsgPubSubListener extends JedisPubSub {  
        @Override  
        public void unsubscribe() {  
            super.unsubscribe();  
        }  
      
        @Override  
        public void unsubscribe(String... channels) {  
            super.unsubscribe(channels);  
        }  
      
        @Override  
        public void subscribe(String... channels) {  
            super.subscribe(channels);  
        }  
      
        @Override  
        public void psubscribe(String... patterns) {  
            super.psubscribe(patterns);  
        }  
      
        @Override  
        public void punsubscribe() {  
            super.punsubscribe();  
        }  
      
        @Override  
        public void punsubscribe(String... patterns) {  
            super.punsubscribe(patterns);  
        }  
      
        @Override  
        public void onMessage(String channel, String message) {  
            System.out.println("channel:" + channel + "receives message :" + message);  
            this.unsubscribe();  
        }  
      
        @Override  
        public void onPMessage(String pattern, String channel, String message) {  
      
        }  
      
        @Override  
        public void onSubscribe(String channel, int subscribedChannels) {  
            System.out.println("channel:" + channel + "is been subscribed:" + subscribedChannels);  
        }  
      
        @Override  
        public void onPUnsubscribe(String pattern, int subscribedChannels) {  
      
        }  
      
        @Override  
        public void onPSubscribe(String pattern, int subscribedChannels) {  
      
        }  
      
        @Override  
        public void onUnsubscribe(String channel, int subscribedChannels) {  
            System.out.println("channel:" + channel + "is been unsubscribed:" + subscribedChannels);  
        }  
    }  

    订阅类的创建

    public class TestSubscribe {  
        @Test  
        public void testSubscribe() throws Exception{  
            Jedis jedis = new Jedis("localhost");  
            RedisMsgPubSubListener listener = new RedisMsgPubSubListener();  
            jedis.subscribe(listener, "redisChatTest");  
            //other code  
        }  
    }  

    发布消息

    Public class TestPublish {  
        @Test  
        public void testPublish() throws Exception{  
            Jedis jedis = new Jedis("localhost");  
            jedis.publish("redisChatTest", "我是天才");  
            Thread.sleep(5000);  
            jedis.publish("redisChatTest", "我牛逼");  
            Thread.sleep(5000);  
            jedis.publish("redisChatTest", "哈哈");  
        }  
    }  
  • 相关阅读:
    CSS边框(圆角、阴影、背景图片)
    CSS3浏览器兼容
    HTML5全局属性
    HTLM5新增属性
    HTML5标签
    如何开始使用bootstrap
    重新了解Java基础(三)-运行机制&HelloWorld
    重新了解Java基础(二)-Java的特性
    Java关键字之native
    重新了解Java基础(一)
  • 原文地址:https://www.cnblogs.com/clovejava/p/8030423.html
Copyright © 2020-2023  润新知