如何实现Java中的延迟队列
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,小编给大家分享一下如何实现Java中的延迟队列,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!常见的实现方法主要有:定时任务扫描、RocketMQ延迟队列、Java自动的延迟队列、监听
千家信息网最后更新 2025年02月01日如何实现Java中的延迟队列
小编给大家分享一下如何实现Java中的延迟队列,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
常见的实现方法主要有:定时任务扫描、RocketMQ延迟队列、Java自动的延迟队列、监听Redis Key过期等等
1. DelayQueue
首先,定义一个延迟任务
package com.cjs.example;import lombok.Data;import java.util.concurrent.Delayed;import java.util.concurrent.TimeUnit;/** * @author ChengJianSheng * @since 2021/3/18 */@Datapublic class DelayTask implements Delayed { private Long orderId; private long expireTime; public DelayTask(Long orderId, long expireTime) { this.orderId = orderId; this.expireTime = expireTime; } @Override public long getDelay(TimeUnit unit) { return expireTime - System.currentTimeMillis(); } @Override public int compareTo(Delayed o) { return (int) (getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS)); }}
然后,定义一个管理类
package com.cjs.example;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;import java.util.concurrent.DelayQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @author ChengJianSheng * @since 2021/3/19 */@Slf4j@Componentpublic class DelayQueueManager implements CommandLineRunner { private DelayQueue queue = new DelayQueue<>(); @Autowired private ParkOrderQueryHandler handler; @Override public void run(String... strings) throws Exception { ExecutorService executorService = Executors.newSingleThreadExecutor(); executorService.execute(new Runnable() { @Override public void run() { while (true) { try { DelayTask task = queue.take(); handler.handle(task); } catch (InterruptedException e) { e.printStackTrace(); } } } }); } public void put(DelayTask task) { queue.put(task); }}
插入任务
@Slf4j@Servicepublic class PayServiceImpl implements PayService { @Autowired private DelayQueueManager delayQueueManager; @Override public void pay() { delayQueueManager.put(new DelayTask(1, 15)); delayQueueManager.put(new DelayTask(2, 30)); delayQueueManager.put(new DelayTask(3, 60)); }}
2. Redis Key过期回调
修改redis.conf文件
# bind 127.0.0.1 -::1
protected-mode no
notify-keyspace-events Ex
[root@localhost redis-6.2.1]$ src/redis-server redis.conf 4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.4 com.example demo0401 0.0.1-SNAPSHOT demo0401 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-maven-plugin
RedisConfig.java
package com.example.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.listener.RedisMessageListenerContainer;/** * @author ChengJianSheng * @since 2021/4/2 */@Configurationpublic class RedisConfig { @Bean public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) { RedisMessageListenerContainer container = new RedisMessageListenerContainer(); container.setConnectionFactory(connectionFactory); return container; }}
创建一个监听类
package com.example.listener;import org.springframework.data.redis.connection.Message;import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;import org.springframework.data.redis.listener.RedisMessageListenerContainer;import org.springframework.stereotype.Component;/** * @author ChengJianSheng * @since 2021/4/2 */@Componentpublic class MyRedisKeyExpirationListener extends KeyExpirationEventMessageListener { public MyRedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) { super(listenerContainer); } @Override public void onMessage(Message message, byte[] pattern) { String expiredKey = message.toString(); System.out.println("监听到Key: " + expiredKey + " 已过期"); }}
看完了这篇文章,相信你对"如何实现Java中的延迟队列"有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
延迟
队列
任务
篇文章
监听
完了
常见
文件
方法
更多
知识
行业
资讯
资讯频道
频道
管理
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
大庆公安局网络安全之队成立
服务器管理器功能在哪里找
HP服务器硬盘管理软件
社旗网络安全周
用宝塔怎么看数据库端口
哈工程网络安全学习
吴忠网络安全平台作业
浙江网络技术服务市场价
电话数据库字段设计
网络安全形势受到各国高度关注
网络安全展架电力
软件开发思维与用户思维
网络意识形态及网络安全责任制
保证数据库安全性
网络安全工程师需要什么
数据库模糊匹配优化
思晨网络技术服务有限公司
php开发需要的软件开发
阿里云建立数据库主机
网络安全过滤及检测系统
数据库多久备份一次
2021我国软件开发现状
程序连接不上sql数据库
江苏联通软件开发面试题
互联网科技公司销售模式
英雄联盟服务器谁负责
三六零网络安全市场空间
社区宣传网络安全新闻
昆明计算机软件开发招聘信息
绍兴软件开发驻场价钱