侧边栏壁纸
博主头像
祥拓软件

欢迎访问,如有问题请发送邮件至:work@work100.com.cn

  • 累计撰写 36 篇文章
  • 累计创建 9 个标签
  • 累计收到 1 条评论

Jedis订阅key过期

祥拓软件
2022-09-06 / 0 评论 / 2 点赞 / 19 阅读 / 184 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-09-06,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
  1. 修改redis配置
    notify-keyspace-events Ex
  2. Subscriber.java
import redis.clients.jedis.JedisPubSub;

/**
 * @Date: 2022/9/6
 */
public class Subscriber extends JedisPubSub {

    @Override
    public void onMessage(String channel, String message) {
        System.out.println(String.format("receive redis published message, channel %s, message %s", channel, message));
    }

    @Override
    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("subscribe redis channel success, channel %s, subscribedChannels %d",
                channel, subscribedChannels));
    }

    @Override
    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("unsubscribe redis channel, channel %s, subscribedChannels %d",
                channel, subscribedChannels));

    }
}

  1. SubThread.java
import cn.hutool.db.nosql.redis.RedisDS;
import redis.clients.jedis.Jedis;

/**
 * @Date: 2022/9/6
 */
public class SubThread extends Thread {

    private final Subscriber subscriber = new Subscriber();

    private final String channel = "__keyevent@10__:expired";

    @Override
    public void run() {
        Jedis jedis = null;
        RedisDS redisDS = null;
        try {
            redisDS = RedisDS.create();
            jedis = redisDS.getJedis();
            jedis.subscribe(subscriber, channel);
        } catch (Exception e) {
            System.out.println(String.format("subsrcibe channel error, %s", e));
        } finally {
            if (null != jedis) {
                jedis.close();
            }
            if (null != redisDS) {
                redisDS.close();
            }
        }
    }

}
2
博主关闭了当前页面的评论