温馨提示×

redis怎么去掉订阅呢

小新
787
2021-02-16 13:03:21
栏目: 云计算

redis怎么去掉订阅呢

redis去掉订阅的方法:

取消订阅是使用监听器类RedisMsgPubSubListener中的unsubscribe方法。

1.创建一个线程类,代码:


package redis.v1.client.thread;


import redis.clients.jedis.Jedis;

import redis.v1.client.common.PubSubCommons;

import redis.v1.client.server.RedisClient;


public class UnsubscribeRunable implements Runnable{


@Override

public void run() {

int i = 1;

while(true) {

try {

Thread.sleep(1000*i);

System.out.println("休眠第"+i+"秒");

i = i*2;

if(i/10 == 0) {

PubSubCommons.listener.unsubscribe("redisChat1");

PubSubCommons.listener.unsubscribe("redisChat");

PubSubCommons.listener.unsubscribe("redisChat2");

}

// PubSubCommons.listener.onPong("redisChat?");

// Jedis jedis = RedisClient.getJedis();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}


}



2.在订阅通道类中加入如下代码即可:


new Thread(new UnsubscribeRunable()).start();




0