LZ一开始配置到启动类里面,结果出现了主线程阻塞的情况。
如下是流程:
首先修改配置文件redis.conf中的:notify-keyspace-events Ex,默认为notify-keyspace-events "" (注释掉默认的,注意notify-keyspace-events Ex之前不能有空格)
启动初始化类里面的contextInitialized方法中加入以下代码:(启动一个线程执行订阅功能)
Thread thread = new Thread(传入需要参数--既然是redis监控必然需要jedis参数);thread.start();
之后再编写业务代码
resource.psubscribe(pubSub, "__keyevent@0__:expired");
这里是订阅监听,__keyevent@0__:expired是监听的频道
redis监听类
public class Listener extends JedisPubSub{ private static Logger logger = Logger.getLogger(Listener.class); private XXX xxx; private YYY yyy; public Listener(XXX xxx, YYY yyy) { this.xxx= xxx; this.yyy= yyy; } @Override public void onPMessage(String pattern, String channel, String message) { logger.info("onPMessage: channel[" + channel + "], message[" + message + "]"); {业务内容} } @Override public void onMessage(String channel, String message) { logger.info("onMessage: channel[" + channel + "], message[" + message + "]"); } @Override public void onSubscribe(String channel, int subscribedChannels) { logger.info("onSubscribe: channel[" + channel + "]," + "subscribedChannels[" + subscribedChannels + "]"); } @Override public void onUnsubscribe(String channel, int subscribedChannels) { logger.info("onUnsubscribe: channel[" + channel + "], " + "subscribedChannels[" + subscribedChannels + "]"); } @Override public void onPUnsubscribe(String pattern, int subscribedChannels) { logger.info("onPUnsubscribe: pattern[" + pattern + "]," + "subscribedChannels[" + subscribedChannels + "]"); } @Override public void onPSubscribe(String pattern, int subscribedChannels) { logger.info("onPSubscribe: pattern[" + pattern + "], " + "subscribedChannels[" + subscribedChannels + "]"); }}
原文地址:https://www.cnblogs.com/autumn007/p/10489776.html
时间: 2024-10-20 11:47:16