过期事件通过Redis的订阅与发布功能(pub/sub)来进行分发。
而对超时的监听呢,并不需要自己发布,只有修改配置文件redis.conf中的:notify-keyspace-events Ex,默认为notify-keyspace-events ""
修改好配置文件后,redis会对设置了expire的数据进行监听,当数据过期时便会将其从redis中删除:
1.先写一个监听器:
package cn.uce.suc.bkg.control;
import redis.clients.jedis.JedisPubSub;
public class KeyExpiredListener extends JedisPubSub{
public void onPSubscribe(String pattern, int subscribedChannels) {
System.out.println("onPSubscribe "
+ pattern + " " + subscribedChannels);
}
@Override
public void onPMessage(String pattern, String channel, String message) {
System.out.println("onPMessage pattern "
+ pattern + " " + channel + " " + message);
}
}
2.订阅者:
package cn.uce.suc.bkg.control;
import java.util.List;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class Subscriber {
public static void main(String[] args) {
JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
Jedis jedis = pool.getResource();
config(jedis);//最重要的部分
jedis.psubscribe(new KeyExpiredListener(), "__keyevent@0__:expired");
}
private static void config(Jedis jedis){
String parameter = "notify-keyspace-events";
List<String> notify = jedis.configGet(parameter);
if(notify.get(1).equals("")){
jedis.configSet(parameter, "Ex"); //过期事件
}
}
}
3.测试类:
package cn.uce.suc.bkg.control;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class TestJedis {
public static void main(String[] args) {
JedisPool pool = new JedisPool(new JedisPoolConfig(), "127.0.0.1");
Jedis jedis = pool.getResource();
//jedis.setex("notify-task-001", 10,"empty");
jedis.set("oneKey", "suhao");
jedis.expire("oneKey", 3);
}
}
4.结果:
先启动订阅者,然后执行测试类,然后等待10秒之后再监听类的方法中就可以获得回调。非常需要主要的时,过期监听的管道默认是__keyevent@0__:expired,艾特后面的0表示第几个是数据库,redis默认的数据库是0~15一共16个数据库。所以如果你存入的数据库是2,那么数据接收的管道就是__keyevent@2__:expired
5.问题:
在先启动订阅者,然后执行测试类时在监听器类onPMessage方法接收不到消息,后来通过查明是因为虽然修改配置文件redis.conf中的:notify-keyspace-events Ex,但是根本没有生效,解决办法是:Subscriber类加入config私有方法判断设置notify-keyspace-events Ex是否生效,否则重新手动设置notify-keyspace-events Ex。