Redisson实现延迟队列

延迟队列是指在队列中存储带有过期时间的数据,在过期时间到达时自动从队列中移除。

延时队列的使用场景:

  • 订单超过15分钟未支付自动取消
  • 推送数据至第三方平台,如果失败重新入队推送,推送间隔时间随着失败次数增加而扩大。
  • 分布式情况下一个定时任务往队列内推,多个实例同时处理任务。

Redisson实现的延迟队列是基于Redis的zset命令实现的,通过将数据存储到zset中,并设置过期时间作为score,通过定时任务轮询zset来实现延迟队列的功能。

以下提供一个 RedisDelayQueueUtil 来简化操作。

java 复制代码
@Slf4j
@Component
public class RedisDelayQueueUtil implements ApplicationContextAware {

    private static RedissonClient redissonClient;

    /**
     * 添加延迟队列
     * @param value 队列值
     * @param delay 延迟时间
     * @param timeUnit 时间单位
     * @param queueCode 队列键
     * @param <T>
     */
    public static <T> void addDelayQueue(T value, long delay, TimeUnit timeUnit, String queueCode){
        try {
            RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque(queueCode);
            RDelayedQueue<Object> delayedQueue = redissonClient.getDelayedQueue(blockingDeque);
            delayedQueue.offer(value, delay, timeUnit);
            log.info("Redisson添加延时队列成功 队列键:{},队列值:{},延迟时间:{}", queueCode, value, timeUnit.toSeconds(delay) + "秒");
        } catch (Exception e) {
            log.error("Redisson添加延时队列失败 {}", e.getMessage());
            throw new RuntimeException("Redisson添加延时队列失败");
        }
    }

    /**
     * 获取延迟队列 - 会阻塞
     * @param queueCode 队列名称
     * @return  <T> 数据
     * @throws InterruptedException
     */
    public static <T> T getDelayQueue(String queueCode) throws InterruptedException {
        RBlockingDeque<T> blockingDeque = redissonClient.getBlockingDeque(queueCode);
        return blockingDeque.take();
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        RedisDelayQueueUtil.redissonClient = applicationContext.getBean(RedissonClient.class);
    }
}
相关推荐
小马哥编程13 小时前
【软考架构】案例分析-分布式锁
分布式·架构
necessary65313 小时前
从工行“余额归零”事件看CAP定理:当金融系统在一致性与可用性之间做出选择
分布式·金融·wpf·可用性测试
维尔切13 小时前
Kafka 概述与安装部署整理
运维·分布式·kafka
罗不俷14 小时前
【Hadoop】Hadoop 起源与核心组件解析 —— 大数据时代的分布式基石
大数据·hadoop·分布式
不会写代码的ys17 小时前
仿RabbitMQ实现消息队列(二)-安装
服务器·分布式·rabbitmq
Microsoft Word17 小时前
Rabbitmq基础篇
网络·分布式·rabbitmq
JavaArchJourney20 小时前
分布式锁方案详解
分布式·后端
会开花的二叉树20 小时前
分布式文件存储服务设计与实现优化
分布式
blammmp1 天前
RabbitMQ的高级特性
分布式·rabbitmq
半旧夜夏1 天前
【分布式缓存】Redis持久化和集群部署攻略
java·运维·redis·分布式·缓存