博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jedis实现订阅发布
阅读量:5297 次
发布时间:2019-06-14

本文共 3148 字,大约阅读时间需要 10 分钟。

1. 发布线程

import java.util.concurrent.TimeUnit;import redis.clients.jedis.Jedis;public class Publish {    public static String CHANNEL ="cctv.news";    public static String[] news = new String[] {"We are brocasting Boxing",                                                                                "We are brocasting night news",                                                                                "We are brocasting Cooking",                                                                                "We are brocating Football"};    public static void getPublish() throws InterruptedException {        Jedis jedis = JedisPoolClass.getResource();        for(String curr:news) {            jedis.publish(CHANNEL, curr);            TimeUnit.SECONDS.sleep(1);        }        jedis.close();    }}

2. 订阅监听类

import redis.clients.jedis.JedisPubSub;public class SubscriberListener extends JedisPubSub{    @Override    public void onMessage(String channel, String message) {        super.onMessage(channel, message);        System.out.println("get message from "+channel+" and the message is"+message);    }    @Override    public void onPMessage(String pattern, String channel, String message) {        super.onPMessage(pattern, channel, message);        System.out.println("get message from "+channel+" and the message is"+message);    }    @Override    public void onPSubscribe(String pattern, int subscribedChannels) {        super.onPSubscribe(pattern, subscribedChannels);        System.out.println("onPubscribe the channel "+subscribedChannels);    }    @Override    public void onPUnsubscribe(String pattern, int subscribedChannels) {        super.onPUnsubscribe(pattern, subscribedChannels);        System.out.println("onPubscribe the channel "+subscribedChannels);    }    @Override    public void onSubscribe(String channel, int subscribedChannels) {        super.onSubscribe(channel, subscribedChannels);        System.out.println("onSubscribe the channel "+subscribedChannels);    }    @Override    public void onUnsubscribe(String channel, int subscribedChannels) {        super.onUnsubscribe(channel, subscribedChannels);        System.out.println("onUnSubscribe the channel "+subscribedChannels);    }    }

3.连接池

import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;public class JedisPoolClass {    private static JedisPool pool = null;    static {        pool = new JedisPool("localhost", 6379);    }    public static Jedis getResource() {        return pool.getResource();    }}

4.测试类

/**     * 必须得先发布再订阅,但是先发布再订阅会导致第一个发布的消息丢失,在实际应用的过程中要考虑到这点     */    private static void PubSubTest() {        Jedis jedis = JedisPoolClass.getResource();        Thread thread = new Thread(new Runnable() {                        @Override            public void run() {                try {                    Publish.getPublish();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        });        thread.start();                jedis.subscribe(new SubscriberListener(), Publish.CHANNEL);    }

转载于:https://www.cnblogs.com/pamCoding/p/9549453.html

你可能感兴趣的文章
CS0103: The name ‘Scripts’ does not exist in the current context解决方法
查看>>
20130330java基础学习笔记-语句_for循环嵌套练习2
查看>>
Spring面试题
查看>>
窥视SP2010--第一章节--SP2010开发者路线图
查看>>
MVC,MVP 和 MVVM 的图示,区别
查看>>
C语言栈的实现
查看>>
代码为什么需要重构
查看>>
TC SRM 593 DIV1 250
查看>>
SRM 628 DIV2
查看>>
2018-2019-2 20165314『网络对抗技术』Exp5:MSF基础应用
查看>>
Python-S9-Day127-Scrapy爬虫框架2
查看>>
使用Chrome(PC)调试移动设备上的网页
查看>>
使用gitbash来链接mysql
查看>>
SecureCRT的使用方法和技巧(详细使用教程)
查看>>
右侧导航栏(动态添加数据到list)
查看>>
81、iOS本地推送与远程推送详解
查看>>
虚拟DOM
查看>>
uva 11468 Substring
查看>>
自建数据源(RSO2)、及数据源增强
查看>>
BootStrap2学习日记2--将固定布局换成响应式布局
查看>>