簡單定時任務解決方案:使用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=63792、在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事件,然后接收到事件后處理相應的業務代碼
新聞熱點
疑難解答