Redis中使用Java代码的方式实现发布订阅流程

场景

Redis 中的发布与订阅的概念与以命令行的方式实现发布订阅举例:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/105042049

在上面了解了 Redis 的发布与订阅的概念以及使用命令行的方式实现简单的发布与订阅流程后,

在 Java 中怎样实现发布与订阅。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

JedisPushSub 类

Jedis 中的 JedisPubSub 类是 Jedis 的一个抽象类,此类定义了 public/subscribe 的回调方法,通过继承 JedisPubSub 类

重写回调方法。实现 java 中 Redis 的发布订阅。当 Redis 发生发布或者订阅的相关事件时会调用这些回调方法,只在

回调方法中实现自己的业务逻辑。

onMessage(): 发布者发布消息时,会执行订阅者的回调方法 onMessage() 接收发布的消息,在此方法实现消息接收后进行

自定义的业务逻辑处理。

实现订阅者

打开 IDEA 新建 Maven 项目,然后添加 jedis 的依赖

    <dependencies>
        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.0.1</version>
        </dependency>
&lt;/dependencies&gt;</pre>

 

然后在 test 新建包,包下新建 Java 类 RedisSubscriber

 

 

然后使其继承 JedisPubSub 并重写 onMessage 方法

在回调方法 onMessage 中实现简单的输出接受的消息和时间。

    @Override
    public void onMessage(String channel, String message) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy MM dd HH🇲🇲ss");
        System.out.println("订阅者:订阅频道 ["+channel+"], 收到消息 ["+message+"], 时间:"+df.format(new Date()));}

然后创建 main 方法,创建 Jedis 对象和订阅者并订阅频道。

    public static void main(String[] args) {
        System.out.println("启动订阅者");
        //创建 Jedis
        Jedis jedis = new Jedis("192.168.40.133", 6379);
        //创建订阅者
        RedisSubscriber redisSubscriber = new RedisSubscriber();
        //订阅频道
        jedis.subscribe(redisSubscriber,"badaodechengxvyuan");}

完整示例代码

package com.badao.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

import java.text.SimpleDateFormat;
import java.util.Date;

public class RedisSubscriber extends JedisPubSub {

@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onMessage(String channel, String message) {
    SimpleDateFormat df </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> SimpleDateFormat(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">yyyy MM dd HH🇲🇲ss</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">订阅者:订阅频道[</span><span style="color: rgba(128, 0, 0, 1)">"</span>+channel+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">],收到消息[</span><span style="color: rgba(128, 0, 0, 1)">"</span>+message+<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">],时间:</span><span style="color: rgba(128, 0, 0, 1)">"</span>+df.format(<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Date()));
}

</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
    System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">启动订阅者</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建Jedis</span>
    Jedis jedis = <span style="color: rgba(0, 0, 255, 1)">new</span> Jedis(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">192.168.40.133</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 128, 1)">6379</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建订阅者</span>
    RedisSubscriber redisSubscriber = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RedisSubscriber();
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">订阅频道</span>
    jedis.subscribe(redisSubscriber,<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">badaodechengxvyuan</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}

}

 

然后运行 main 方法,启动该订阅者。

 

 

参照上面以命令行的方式实现发布和订阅的流程,在上面发布一个消息

我们就可以在此订阅者中接收到消息了。

 

 

实现发布者

再次新建一个 Maven 项目,然后引入 jedis 的依赖,同上新建包和类

在类中新建 main 方法,然后发布消息

package com.badao.redis;

import redis.clients.jedis.Jedis;

public class Publisher {
public static void main(String[] args) {
System.
out.println("开始发布......");
//创建 Jedis
Jedis jedis = new Jedis("192.168.40.133", 6379);
//发布消息
jedis.publish("badaodechengxvyuan", "紧急通知:...");
System.
out.println("消息发送完毕......");
}
}

然后运行 main 方法,就可以在前面的订阅者的项目中收到消息了