SpringBoot使用Redisson做延迟队列案列(超详细)
springboot项目使用redisson做延时队列。
·
背景:
有些场景下,需要延迟触发一些任务。比如:延迟几秒钟发送短信或者邮件;某些业务系统回调,需要延时几秒钟后回调。
当然,实现延时触发的方式有很多,我这里采用 redisson 的 RDelayedQueue,一是因为接入简单,二是没有分布式的问题。毕竟现在微服务大行其道。
接下来,我们进入正题。
使用步骤:
使用 延迟队列呢,一般就几个步骤:
- 添加消息进入延迟队列
- 从延迟队列中取出消息消费
具有步骤:
- 新建springBoot项目,并导入pom.xml相关依赖
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--Redisson支持--> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.12.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
- 项目结构如下
调试
1.正常情况
2.我调用完接口,redisson突然down调,消息依然能正常被消费
我的demo也贡献给大家,有需要的可以随时去下载
Gitee地址:redisson延时队列demo: springBoot项目使用redisson做延迟队列
更多推荐
已为社区贡献2条内容
所有评论(0)