redisTemplate處理/獲取redis消息隊列,
參考代碼
/** * redis消息隊列 */ @Component public class RedisQueue { @Autowired private RedisTemplate redisTemplate; /** ---------------------------------- redis消息隊列 ---------------------------------- */ /** * 存值 * @param key 鍵 * @param value 值 * @return */ public boolean lpush(String key, Object value) { try { redisTemplate.opsForList().leftPush(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 取值 - <rpop:非阻塞式> * @param key 鍵 * @return */ public Object rpop(String key) { try { return redisTemplate.opsForList().rightPop(key); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 取值 - <brpop:阻塞式> - 推薦使用 * @param key 鍵 * @param timeout 超時時間 * @param timeUnit 給定單元粒度的時間段 * TimeUnit.DAYS //天 * TimeUnit.HOURS //小時 * TimeUnit.MINUTES //分鍾 * TimeUnit.SECONDS //秒 * TimeUnit.MILLISECONDS //毫秒 * @return */ public Object brpop(String key, long timeout, TimeUnit timeUnit) { try { return redisTemplate.opsForList().rightPop(key, timeout, timeUnit); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 查看值 * @param key 鍵 * @param start 開始 * @param end 結束 0 到 -1代表所有值 * @return */ public List<Object> lrange(String key, long start, long end) { try { return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { e.printStackTrace(); return null; } } }