zoukankan      html  css  js  c++  java
  • springboot使用redis的keyspace notifications 实现定时通知

    简单定时任务解决方案:使用redis的keyspace notifications(键失效后通知事件)
    需要注意此功能是在redis 2.8版本以后推出的,因此你服务器上的reids最少要是2.8版本以上;

    1.开启redis key过期提醒
    修改redis相关事件配置。找到redis配置文件redis.conf,查看“notify-keyspace-events”的配置项,如果没有,添加“notify-keyspace-events Ex”,如果有值,添加Ex,相关参数说明如下:
    K:keyspace事件,事件以__keyspace@<db>__为前缀进行发布;         
    E:keyevent事件,事件以__keyevent@<db>__为前缀进行发布;         
    g:一般性的,非特定类型的命令,比如del,expire,rename等;        
    $:字符串特定命令;         
    l:列表特定命令;         
    s:集合特定命令;         
    h:哈希特定命令;         
    z:有序集合特定命令;         
    x:过期事件,当某个键过期并删除时会产生该事件;         
    e:驱逐事件,当某个键因maxmemore策略而被删除时,产生该事件;         
    A:g$lshzxe的别名,因此”AKE”意味着所有事件。
    

    2.在springboot中使用   

         <!-- redis -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    

    3.定义配置RedisListenerConfig 

    @Configuration
    public class RedisTimerConfiguration {
    
        private Logger logger = LoggerFactory.getLogger(RedisTimerConfiguration.class);
        @Autowired
        private RedisConnectionFactory redisConnectionFactory;
        @Bean
        public RedisMessageListenerContainer redisMessageListenerContainer() {
            RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
            redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
            return redisMessageListenerContainer;
        }
    }

    4.定义监听器,实现KeyExpirationEventMessageListener接口,查看源码发现,该接口监听所有db的过期事件keyevent@*:expired"

    @Component
    public class RedisTask extends KeyExpirationEventMessageListener {
    
        private Logger logger = LoggerFactory.getLogger(RedisTask.class);
    
        public RedisTask(RedisMessageListenerContainer listenerContainer) {
            super(listenerContainer);
        }
        @Override
        public void onMessage(Message message, byte[] pattern) {
            String channel = new String(message.getChannel(), StandardCharsets.UTF_8);
            //过期的key
            String key = new String(message.getBody(),StandardCharsets.UTF_8);
            logger.info("redis key 过期:pattern={},channel={},key={}",new String(pattern),channel,key);
           
            }
        }  

     

    kafka rabbitMq
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    密码学-学习笔记
    让MySQL速度提升3倍的19种优化方式
    Oracle/云MySQL/MsSQL“大迁移”真相及最优方案
    tf.TFRecordReader()函数解析(最清晰的解释)
    Polkadot(波卡)简介
    Gavin Wood的故事:神级黄皮书、出走以太坊、乱世成名与三代区块链
    从设计哲学对比波卡与Cosmos
  • 原文地址:https://www.cnblogs.com/stt101517/p/11527825.html
Copyright © 2011-2022 走看看