国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

springboot使用redis的keyspace notifications

2019-11-08 03:13:20
字體:
來源:轉載
供稿:網友
簡單定時任務解決方案:使用redis的keyspace notifications(鍵失效后通知事件)需要注意此功能是在redis 2.8版本以后推出的,因此你服務器上的reids最少要是2.8版本以上;(A)業務場景:1、當一個業務觸發以后需要啟動一個定時任務,在指定時間內再去執行一個任務2、redis的keyspace notifications 會在key失效后發送一個事件,監聽此事件的的客戶端就可以收到通知(B)服務準備:    1、修改reids配置文件(redis.conf)        redis默認不會開啟keyspace notifications,因為開啟后會對cpu有消耗        備注:            E:keyevent事件,事件以__keyevent@<db>__為前綴進行發布;            x:過期事件,當某個鍵過期并刪除時會產生該事件;        配置如下:            notify-keyspace-events "Ex"    2、重啟redis就可以了,我們測試一下我們有沒有修改成功。    開啟一個客戶端用來監聽:        [test@127.0.0.1 ~]$ redis-cli        127.0.0.1:6379> psubscribe __keyevent@0__:expired        Reading messages... (PRess Ctrl-C to quit)        1) "psubscribe"        2) "__keyevent@0__:expired"        3) (integer) 1    開啟一個發送key的客戶端:        [test@127.0.0.1 ~]$ redis-cli        127.0.0.1:6379> set testKey 123 PX 100        OK        127.0.0.1:6379>    結果:        [test@127.0.0.1 ~]$ redis-cli        127.0.0.1:6379> psubscribe __keyevent@0__:expired        Reading messages... (press Ctrl-C to quit)        1) "psubscribe"        2) "__keyevent@0__:expired"        3) (integer) 1        1) "pmessage"        2) "__keyevent@0__:expired"        3) "__keyevent@0__:expired"        4) "testKey"    這樣代表測試成功!(C)springBoot工程引用    1、配置文件 application.properties加入如下配置
spring.redis.host=127.0.0.1spring.redis.pool.max-active=8spring.redis.pool.max-idle=8spring.redis.pool.max-wait=-1spring.redis.pool.min-idle=0spring.redis.timeout=3000spring.redis.passWord=123456spring.redis.port=6379
    2、在com.xxx.xxx下添加RedisConfiguration類 
package com.xxx.xxx;        import org.slf4j.Logger;        import org.slf4j.LoggerFactory;        import org.springframework.beans.factory.annotation.Value;        import org.springframework.cache.annotation.CachingConfigurerSupport;        import org.springframework.context.annotation.Bean;        import org.springframework.stereotype.Component;        import redis.clients.jedis.JedisPool;        import redis.clients.jedis.JedisPoolConfig;        /**         * Created by test on 2017/2/12.         */        @Component        public class RedisConfiguration extends CachingConfigurerSupport {            Logger logger = LoggerFactory.getLogger(RedisConfiguration.class);            @Value("${spring.redis.host}")            private String host;            @Value("${spring.redis.port}")            private int port;            @Value("${spring.redis.timeout}")            private int timeout;            @Value("${spring.redis.pool.max-idle}")            private int maxIdle;            @Value("${spring.redis.pool.max-wait}")            private long maxWaitMillis;            @Value("${spring.redis.password}")            private String password;            @Bean            public JedisPool redisPoolFactory() {                logger.info("JedisPool注入成功!!");                logger.info("redis地址:" + host + ":" + port);                JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();                jedisPoolConfig.setMaxIdle(maxIdle);                jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);                JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password);                return jedisPool;            }        }    3、在com.xxx.xxx下創建redisKeyspace包    并創建一個Subscribe.java類        package com.xxx.xxx.redisKeyspace;        import org.apache.commons.logging.Log;        import org.apache.commons.logging.LogFactory;        import redis.clients.jedis.JedisPubSub;        /**         * Created by test on 2017/2/12.         */        public class Subscribe extends JedisPubSub {            private static final Log log= LogFactory.getLog(Subscribe.class);            // 初始化按表達式的方式訂閱時候的處理            public void onPSubscribe(String pattern, int subscribedChannels) {                log.info("Subscribe-onPSubscribe>>>>>>>>>>>>>>>>>>>>>>>>"+pattern + "=" + subscribedChannels);            }            // 取得按表達式的方式訂閱的消息后的處理            public void onPMessage(String pattern, String channel, String message) {                try {                    log.info(pattern + "=" + channel + "=" + message);                    //在這里寫你相關的邏輯代碼                }catch (Exception e){                    e.printStackTrace();                }            }        }           
3、創建SubscribeThread.java線程類    備注:此線程實現了CommandLineRunner,springBoot的CommandLineRunner 可以達到工程已啟動就開啟一個線程        
package com.xxx.xxx.redisKeyspace;        import org.apache.commons.logging.Log;        import org.apache.commons.logging.LogFactory;        import org.springframework.beans.factory.annotation.Autowired;        import org.springframework.boot.CommandLineRunner;        import org.springframework.stereotype.Component;        import redis.clients.jedis.Jedis;        import redis.clients.jedis.JedisPool;        /**         * Created by test on 2017/2/13.         */        @Component        public class SubscribeThread implements CommandLineRunner {            private Log log= LogFactory.getLog(SubscribeThread.class);            @Autowired            JedisPool jedisPool;            @Override            public void run(String... strings) throws Exception {                Jedis jedis= jedisPool.getResource();                try {                    //監聽所有reids通道中的過期事件                    jedis.psubscribe(new Subscribe(), "*");                } catch (Exception e) {                    jedis.close();                    e.printStackTrace();                }finally {                    jedis.close();                }            }        }    4、具體業務中向redis中添加key        
/**         * 添加redis秒定時任務         * @param seconds 秒         */        private void addRedisTimer(String key,int seconds) {            Jedis jedis = jedisPool.getResource();            try {                if (!jedis.exists(key))                    jedis.setex(key,seconds,"");            }catch (Exception e){                logger.error("addRedisTimer error: ", e);            }finally {                jedis.close();            }        }這樣我們就實現了springboot已啟動就開啟一個線程用來監聽reids的keyspace notifications事件,然后接收到事件后處理相應的業務代碼
上一篇:類屬(Generic)

下一篇:Crackme 1

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平罗县| 亳州市| 穆棱市| 嘉鱼县| 游戏| 得荣县| 颍上县| 仪征市| 石城县| 成都市| 永修县| 屏东县| 长岭县| 三原县| 平阳县| 宜兴市| 富锦市| 宝山区| 滦平县| 石屏县| 茌平县| 永清县| 榕江县| 海门市| 霸州市| 黄冈市| 德惠市| 包头市| 广水市| 新河县| 神池县| 将乐县| 涡阳县| 宜城市| 武乡县| 泽普县| 瑞昌市| 盱眙县| 永仁县| 九龙城区| 南阳市|