Redisson实现延迟队列

编程入门 行业动态 更新时间:2024-10-12 05:47:31

Redisson实现延迟<a href=https://www.elefans.com/category/jswz/34/1771257.html style=队列"/>

Redisson实现延迟队列

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

延时队列的使用场景:

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

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

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

@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();}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {RedisDelayQueueUtil.redissonClient = applicationContext.getBean(RedissonClient.class);}
}

更多推荐

Redisson实现延迟队列

本文发布于:2023-11-16 01:21:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1611025.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   Redisson

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!