redisTemplate處理/獲取redis消息隊列


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

}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM