pom.xml
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
</dependency>
Jedis 连接配置工具类
import redis.clients.jedis.Jedis;
public class ChannelUtils {
public static Jedis getChannelInstance() {
// Redis IP及端口号
Jedis jedis = new Jedis("127.0.0.1", 6380);
// Redis 密码
jedis.auth("password");
// Redis 用户通信的数据库ID
jedis.select(1);
return jedis;
}
}
生产者发布消息
import redis.clients.jedis.Jedis;
import java.util.Scanner;
public class Producer {
/**
* 频道名称(需与消费者保持一致)
*/
private static final String channel = "CHANNEL";
public static void main(String[] args) {
// 获取连接
Jedis jedis = ChannelUtils.getChannelInstance();
while (true) {
System.out.print("请输入即将发布的消息:");
Scanner str = new Scanner(System.in);
// 发布消息
publish(jedis, channel, str.next());
System.out.println("消息已发布,请查看订阅者状态");
}
}
/**
* 功能描述:
* <生产者发布消息>
*
* @param jedis 1
* @param channel 2
* @param msg 3
* @return void
* @author zhoulipu
* @date 2019/8/1 2:45
*/
public static void publish(Jedis jedis, String channel, String msg) {
jedis.publish(channel, msg);
}
}
消费者订阅频道
import redis.clients.jedis.Jedis;
public class Consumer {
/**
* 频道名称(需与消费者保持一致)
*/
private static final String channel = "CHANNEL";
public static void main(String[] args) {
// 获取连接
Jedis jedis = ChannelUtils.getChannelInstance();
// 建立监听
RedisMsgPubSubListener listener = new RedisMsgPubSubListener();
// 订阅频道
subscribe(jedis, listener, channel);
}
/**
* 功能描述:
* <订阅频道>
*
* @param jedis 1
* @param listener 2
* @param channel 3
* @return void
* @author zhoulipu
* @date 2019/8/1 3:04
*/
public static void subscribe(Jedis jedis, RedisMsgPubSubListener listener, String channel) {
jedis.subscribe(listener, channel);
}
}
消息监听
import redis.clients.jedis.JedisPubSub;
public class RedisMsgPubSubListener extends JedisPubSub {
/**
* 功能描述:
* <订阅订阅频道>
*
* @param channels 1
* @return void
* @author zhoulipu
* @date 2019/8/1 2:51
*/
@Override
public void subscribe(String... channels) {
super.subscribe(channels);
}
/**
* 功能描述:
* <取消订阅频道>
*
* @param channels 1
* @return void
* @author zhoulipu
* @date 2019/8/1 2:51
*/
@Override
public void unsubscribe(String... channels) {
super.unsubscribe(channels);
}
/**
* 功能描述:
* <订阅订阅模式>
*
* @param patterns 1
* @return void
* @author zhoulipu
* @date 2019/8/1 2:51
*/
@Override
public void psubscribe(String... patterns) {
super.psubscribe(patterns);
}
/**
* 功能描述:
* <取消订阅模式>
*
* @param patterns 1
* @return void
* @author zhoulipu
* @date 2019/8/1 2:51
*/
@Override
public void punsubscribe(String... patterns) {
super.punsubscribe(patterns);
}
/**
* 功能描述:
* <订阅频道>
*
* @param channel 1当前的频道
* @param subscribedChannels 2总共订阅的频道数量
* @return void
* @author zhoulipu
* @date 2019/8/1 2:51
*/
@Override
public void onSubscribe(String channel, int subscribedChannels) {
System.out.println("onSubscribe-channel:" + channel + " is been subscribed:" + subscribedChannels);
}
/**
* 功能描述:
* <监听到订阅频道收到消息>
*
* @param channel 1当前的频道
* @param message 2当前收到的消息
* @return void
* @author zhoulipu
* @date 2019/8/1 2:51
*/
@Override
public void onMessage(String channel, String message) {
System.out.println("onMessage-channel:" + channel + " message :" + message);
}
/**
* 功能描述:
* <取消订阅频道>
*
* @param channel 1当前的频道
* @param subscribedChannels 2总共订阅的频道数量
* @return void
* @author zhoulipu
* @date 2019/8/1 2:51
*/
@Override
public void onUnsubscribe(String channel, int subscribedChannels) {
System.out.println("onUnsubscribe-channel:" + channel + " is been subscribed:" + subscribedChannels);
}
/**
* 功能描述:
* <订阅频道模式时的回调>
*
* @param pattern 1
* @param subscribedChannels 2
* @return void
* @author zhoulipu
* @date 2019/8/1 2:51
*/
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
System.out.println("onPUnsubscribe-pattern:" + pattern + " is been subscribed:" + subscribedChannels);
}
/**
* 功能描述:
* <监听到订阅模式接受到消息时的回调>
*
* @param pattern 1
* @param channel 2
* @param message 3
* @return void
* @author zhoulipu
* @date 2019/8/1 2:50
*/
@Override
public void onPMessage(String pattern, String channel, String message) {
System.out.println("onPMessage-pattern:" + pattern + " channel:" + channel + " message :" + message);
}
/**
* 功能描述:
* <取消订阅模式时的回调>
*
* @param pattern 1
* @param subscribedChannels 2
* @return void
* @author zhoulipu
* @date 2019/8/1 2:50
*/
@Override
public void onPUnsubscribe(String pattern, int subscribedChannels) {
System.out.println("onPUnsubscribe-pattern:" + pattern + " is been subscribed:" + subscribedChannels);
}
}