Redisson 延迟队列

目录

1、延迟队列枚举

2、延迟队列工具类

3、延迟队列启动类

4、延迟队列执行器接口

5、延迟队列执行类

6、添加延迟消息


1、延迟队列枚举

@Getter
@AllArgsConstructor
public enum RedisDelayQueueEnum {
    /**
     * 运营通知
     */
    OPERATING_NOTIFY("OPERATING_NOTIFY", "运营通知", "operatingNotifyListener");

    private final String queueCode;
    private final String desc;
    private final String beanId;
}

2、延迟队列工具类

@Slf4j
@Component
@RequiredArgsConstructor
public class RedisDelayQueueUtil {
    private final RedissonClient redissonClient;

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

    /**
     * 获取延迟队列
     *
     * @param queueCode 队列键
     * @return 队列值
     */
    public <T> T getDelayQueue(String queueCode) throws InterruptedException {
        RBlockingDeque blockingDeque = redissonClient.getBlockingDeque(queueCode);
        redissonClient.getDelayedQueue(blockingDeque);
        T value = (T) blockingDeque.take();
        return value;
    }
}

3、延迟队列启动类

@Slf4j
@Component
@RequiredArgsConstructor
public class RedisDelayQueueRunner implements CommandLineRunner {
    private final RedisDelayQueueUtil redisDelayQueueUtil;

    @Override
    public void run(String... args) throws Exception {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
                .setNameFormat("redis-delay-%d").build();
        ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());

        singleThreadPool.execute(() -> {
            while (true) {
                RedisDelayQueueEnum[] queueArray = RedisDelayQueueEnum.values();
                for (RedisDelayQueueEnum queue : queueArray) {
                    try {
                        Object o = redisDelayQueueUtil.getDelayQueue(queue.getQueueCode());
                        if (null != o) {
                            RedisDelayQueueExecutor redisDelayQueueHandle = SpringContextHolder.getBean(queue.getBeanId());
                            redisDelayQueueHandle.execute(o);
                        }
                    } catch (Exception e) {
                        log.error("Redis延迟队列{}异常中断:{}", queue.getDesc(), e.getMessage(), e);
                    }
                }
            }
        });

        log.info("Redis延迟队列启动成功");
    }

}

4、延迟队列执行器接口

public interface RedisDelayQueueExecutor<T> {

    /**
     * 执行
     * @param t 消息
     * @author shiweijia
     * @date 2021/8/6 16:46
     */
    void execute(T t);
}

5、延迟队列执行类

@Slf4j
@Component
@RequiredArgsConstructor
public class OperatingNotifyListener implements RedisDelayQueueExecutor<String> {

    @Override
    public void execute(String message) {
        log.info("运营通知消息:{}", message);
    }
}

6、添加延迟消息

redisDelayQueueUtil.addDelayQueue("运营通知消息", 60, TimeUnit.SECONDS, RedisDelayQueueEnum.OPERATING_NOTIFY.getQueueCode());

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ronshi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值