NutzCN Logo
问答 请教一个redis key过期订阅的问题!
发布于 2673天前 作者 daydayup 2714 次浏览 复制 上一个帖子 下一个帖子
标签:

我订阅了redis的一个key过期订阅的消息,在程序刚启动的时候能够正常获取到消息,但是过一段时间后就再也无法获取到消息了,需要重启程序!
猜测是不是因为连接空闲时间太长,但是连接没有断开。是不是需要设置心跳时间,是的话,到怎么设置

13 回复
        RedisService RedisService= config.getIoc().get(RedisService.class);
            new Thread() {
                public void run() {
                    TopicMessageListener listener = new TopicMessageListener();
                    log.info("开始监听Redis事件......");
                    RedisService.psubscribe(listener, "__keyevent@0__:expired");
                }
            }.start();

public class TopicMessageListener extends JedisPubSub {

private static final Log logger = Logs.get();

// 取得订阅的消息后的处理
public void onMessage(String channel, String message) {
    // System.out.println(channel + "=" + message);
    logger.infof("收到redis事件onMessage:%s = %s", channel, message);
}

// 取得按表达式的方式订阅的消息后的处理
public void onPMessage(String pattern, String channel, String message) {
    logger.infof("收到redis事件onPMessage:%s = %s = %s", pattern, channel, message);
}


public static void main(String[] args) {
    JedisPool jedisPool =new JedisPool("119.23.144.5",6379);
        new Thread() {
        public void run() {
            while (true){
                TopicMessageListener listener = new TopicMessageListener();
                System.out.println("开始监听Redis事件......");
                jedisPool.getResource().psubscribe(listener, "__keyevent@0__:expired");
            }
        }

    }.start();
}

}

要加try-catch和while true的,抛异常的时候就要重连

你参考一下nutz-plugins-cache里面的代码吧

@wendal
加了try-catch和while true 但是过一会并没有进catch 块 这样也不行 。还是过几分钟之后就收不到了
JedisPool jedisPool =new JedisPool("119.23.144.5",6379);
new Thread() {
public void run(){
while (true){
logger.info("开始订阅redis---------");
TopicMessageListener listener = new TopicMessageListener();
Jedis redis = jedisPool.getResource();
try {
redis.psubscribe(listener, "__keyevent@0__:expired");
}catch(JedisConnectionException e){
logger.warn("Exception :", e);
logger.warn("Exit redis psubscribe, retry after 1 second");
}catch(Exception e){
logger.error("Exception:", e);
}
try{
Thread.sleep(1000);
}catch(Exception unused){

                }
                try{
                    if(redis != null){
                        redis.close();
                    }
                }catch(Exception unused){

                }
            }
        }

    }.start();

JedisPool jedisPool =new JedisPool("119.23.144.5",6379);
new Thread() {
public void run(){
while (true){
logger.info("开始订阅redis---------");
TopicMessageListener listener = new TopicMessageListener();
Jedis redis = jedisPool.getResource();
try {
redis.psubscribe(listener, "__keyevent@0__:expired");
}catch(JedisConnectionException e){
logger.warn("Exception :", e);
logger.warn("Exit redis psubscribe, retry after 1 second");
}catch(Exception e){
logger.error("Exception:", e);
}
try{
Thread.sleep(1000);
}catch(Exception unused){

                }
                try{
                    if(redis != null){
                        redis.close();
                    }
                }catch(Exception unused){

                }
            }
        }

    }.start();

public static void main(String[] args) {
JedisPool jedisPool =new JedisPool("119.23.144.5",6379);
new Thread() {
public void run(){
while (true){
logger.info("开始订阅redis---------");
TopicMessageListener listener = new TopicMessageListener();
Jedis redis = jedisPool.getResource();
try {
redis.psubscribe(listener, "__keyevent@0__:expired");
}catch(JedisConnectionException e){
logger.warn("Exception :", e);
logger.warn("Exit redis psubscribe, retry after 1 second");
}catch(Exception e){
logger.error("Exception:", e);
}
try{
Thread.sleep(1000);
}catch(Exception unused){

                }
                try{
                    if(redis != null){
                        redis.close();
                    }
                }catch(Exception unused){

                }
            }
        }

    }.start();
}

JedisPool jedisPool =new JedisPool("119.23.144.5",6379);
new Thread() {
public void run(){
while (true){
logger.info("开始订阅redis---------");
TopicMessageListener listener = new TopicMessageListener();
Jedis redis = jedisPool.getResource();
try {
redis.psubscribe(listener, "__keyevent@0__:expired");
}catch(JedisConnectionException e){
logger.warn("Exception :", e);
logger.warn("Exit redis psubscribe, retry after 1 second");
}catch(Exception e){
logger.error("Exception:", e);
}
try{
Thread.sleep(1000);
}catch(Exception unused){

                }
                try{
                    if(redis != null){
                        redis.close();
                    }
                }catch(Exception unused){

                }
            }
        }

    }.start();
}
public JedisPool jedisPool =new JedisPool("119.23.144.5",6379);
public void execute() {
    while (true) {
        try {
                     TopicMessageListener listener = new TopicMessageListener();
                     Jedis redis = jedisPool.getResource();
                     redis.psubscribe(listener, "__keyevent@0__:expired");
        } catch(Throwable e) {
            log.info(e.getMessage(), e);
        }
        Lang.quiteSleep(1000);
    }
}

试了一下,还是过几分钟就收不到连

添加回复
请先登陆
回到顶部