码迷,mamicode.com
首页 > 其他好文 > 详细

Jboot实现Redis操作事件通知

时间:2020-03-25 12:01:39      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:this   bst   效果   ann   channels   star   end   sage   col   

 

redis.conf

notify-keyspace-events Ex

 

 

KeyExpiredListener.java
public class KeyExpiredListener extends JedisPubSub {
    public KeyExpiredListener() {
    }

    @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));
    }
}

 

AppListener.java
public abstract class AppListener extends JbootAppListenerBase {

    @Override
    public void onInterceptorConfig(Interceptors interceptors) {
        super.onInterceptorConfig(interceptors);
        interceptors.add(new SessionInViewInterceptor());
    }

    @Override
    public void onEngineConfig(Engine me) {

    }

    @Override
    public void onStart() {
        Aop.inject(this);

        Jboot.getRedis().subscribe(new KeyExpiredListener(), "__keyevent@0__:expired");
    }

}

 

测试代码

Cache.set("test-101", "101", 30);
Cache.set("test-201", "201", 60);
Cache.set("test-301", "301", 90);

 

效果

receive redis published message, channel __keyevent@0__:expired, message xxxx:S:test-101
receive redis published message, channel __keyevent@0__:expired, message xxxx:S:test-201
receive redis published message, channel __keyevent@0__:expired, message xxxx:S:test-301

 

Jboot实现Redis操作事件通知

标签:this   bst   效果   ann   channels   star   end   sage   col   

原文地址:https://www.cnblogs.com/coffee_cn/p/12565112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!