在redis-cli中使用Pub/Sub
订阅
发送
接收
通配符的Pub/Sub
Redis还支持通配符的订阅和发布。客户端可以订阅满足一个或多个规则的channel消息,相应的命令是PSUBSCRIBE和PUNSUBSCRIBE。接下来我们再用另一个redis-cli客户端来订阅"chi*"的channel
Java中使用
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>
- 监听器
package com.ghgcn.redis.test01;
import redis.clients.jedis.JedisPubSub;
/**
* jredis 监听
*
* @author 刘楠
* @date 2019年9月23日 下午2:35:46
* @version 1.0
*/
public class JedisPus extends JedisPubSub {
@Override
public void onMessage(String channel, String message) {
System.out.println("onMessage Channel:" + channel + ",Message:" + message);
}
@Override
public void onPMessage(String pattern, String channel, String message) {
System.out.println("onPMessage Pattern:" + pattern + ",Channel:" + channel + ",Message:" + message);
}
@Override
public void onSubscribe(String channel, int subscribedChannels) {
System.out.println(" onSubscribe---channel:"+channel+",subscribedChannels:"+subscribedChannels);
}
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
System.out.println(" onPUnsubscribe---pattern:"+pattern+",subscribedChannels:"+subscribedChannels);
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
System.out.println("onPSubscribe---pattern:"+pattern+",subscribedChannels:"+subscribedChannels);
}
}
发布者
package com.ghgcn.redis.test01;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import redis.clients.jedis.Jedis;
public class Publisher {
private Jedis jedis;
private String channel;
public Publisher(Jedis jedis, String channel) {
this.jedis = jedis;
this.channel = channel;
}
public void startPublish() {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("请输入message:");
String line = reader.readLine();
if (!"quit".equals(line)) {
jedis.publish(channel, line);
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行
package com.ghgcn.redis.test01;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class PubMain {
//Channel
public static final String CHANNEL = "mytestchannel";
//redis连接地址
public static final String HOST = "192.168.99.100";
//端口
public static final int PORT = 32768;
//配置
private static JedisPoolConfig poolConfig = new JedisPoolConfig();
//连接池
private static JedisPool jedisPool = new JedisPool(poolConfig, HOST, PORT);
public static void main(String[] args) {
//发送者
final Jedis publisherJedis = jedisPool.getResource();
//订阅者
final Jedis subscriberJedis = jedisPool.getResource();
//监听器
JedisPus jedisPus = new JedisPus();
//启动线程
new Thread(new Runnable() {
public void run() {
try {
System.out.println("开始订阅。。。。。。。。");
subscriberJedis.subscribe(jedisPus, CHANNEL);
System.out.println("订阅结束。。。。。。。。。");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
//开始发送消息
new Publisher(publisherJedis, CHANNEL).startPublish();
//关闭连接
publisherJedis.close();
//取消订阅
jedisPus.unsubscribe();
//订阅者关闭
subscriberJedis.close();
}
}
- 结束
开始订阅。。。。。。。。
请输入message:
onSubscribe---channel:mytestchannel,subscribedChannels:1
33
请输入message:
onMessage Channel:mytestchannel,Message:33
44
请输入message:
onMessage Channel:mytestchannel,Message:44
55
请输入message:
onMessage Channel:mytestchannel,Message:55
66
请输入message:
onMessage Channel:mytestchannel,Message:66
77
请输入message:
onMessage Channel:mytestchannel,Message:77
88
请输入message:
onMessage Channel:mytestchannel,Message:88
99
请输入message:
onMessage Channel:mytestchannel,Message:99