Java 延迟队列的常用的实现方式
作者:废物大师兄 时间:2022-06-30 13:57:00
延迟队列的使用场景还比较多,例如:
1、超时未收到支付回调,主动查询支付状态;
2、规定时间内,订单未支付,自动取消;
。。。
总之,但凡需要在未来的某个确定的时间点执行检查的场景中都可以用延迟队列。
常见的手段主要有:定时任务扫描、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
*/
@Data
public 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
@Component
public class DelayQueueManager implements CommandLineRunner {
private DelayQueue<DelayTask> 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
@Service
public 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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo0401</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo0401</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
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
*/
@Configuration
public 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
*/
@Component
public 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 + " 已过期");
}
}
3. RocketMQ
官方文档:https://help.aliyun.com/document_detail/29549.htm
来源:https://www.cnblogs.com/cjsblog/p/14612169.html
标签:Java,延迟队列
0
投稿
猜你喜欢
Android Studio使用USB真机调试详解
2022-05-23 00:49:40
详解JAVA中的for-each循环与迭代
2022-05-11 12:29:14
C#中DataTable 转换为 Json的方法汇总(三种方法)
2021-12-12 16:41:00
c++与c#的时间转换示例分享
2022-08-22 07:21:40
Java ExecutorService四种线程池使用详解
2023-05-17 07:12:56
Java持久层框架MyBatis简单实例
2023-01-09 08:43:16
spring boot补习系列之几种scope详解
2022-06-10 13:39:13
C#单例模式(Singleton Pattern)实例教程
2022-11-23 10:44:05
Java 爬虫工具Jsoup详解
2022-04-11 03:46:16
Android开发中使用Intent打开第三方应用及验证可用性的方法详解
2021-08-12 19:28:47
Android listview的滑动冲突解决方法
2022-07-19 02:50:59
关于Future机制原理及解析
2022-01-23 03:57:48
C#开发WinForm根据条件改变DataGridView行颜色
2022-05-06 05:38:56
使用IntelliJ IDEA搭建SSM框架的图文教程
2022-06-14 00:56:19
Java中的InputStreamReader和OutputStreamWriter源码分析_动力节点Java学院整理
2022-10-13 10:46:53
springboot配置https访问的方法
2022-12-11 16:17:37
SpringBoot Pom文件依赖及Starter启动器详细介绍
2022-10-08 19:30:20
Java中的异常和处理机制实例详解
2022-08-10 06:03:19
简介Java的Hibernate框架中的Session和持久化类
2023-04-17 14:41:13
解决微服务中关于用户token处理到的坑
2022-05-21 08:31:03