本文主要是介绍使用DelayQueue的实现延时任务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、背景
项目中经常会用到类似一些需要延迟执行的功能,比如缓存。java提供了DelayQueue来很轻松的实现这种功能。Delayed接口中的getDelay方法返回值小于等于0的时候,表示时间到达,可以从DelayQueue中通过take()方法取的到期的对象。到期对象是实现了Delayed的类。
2、demo
2.1 依赖配置
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--非必须--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.0.M1</version></dependency><!--非必须--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version></dependency><!--非必须--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
2.2 整体架构
** 工具类:**
执行任务所需的基础参数
import lombok.Data;@Data
public class TaskBase {//任务参数,根据业务需求多少都行private String identifier;public TaskBase(String identifier) {this.identifier = identifier;}
}
执行的任务和时间
import cn.hutool.core.date.DateUtil;import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
/*** 延时任务*/
public class DelayTask implements Delayed {//任务参数final private TaskBase data;//任务的延时时间,单位毫秒final private long expire;/*** 构造延时任务** @param data 业务数据* @param expire 任务延时时间(ms)*/public DelayTask(TaskBase data, long expire) {super();this.data = data;this.expire = expire + System.currentTimeMillis();}public TaskBase getData() {return data;}public long getExpire() {return expire;}@Overridepublic boolean equals(Object obj) {if (obj instanceof DelayTask) {return this.data.getIdentifier().equals(((DelayTask) obj).getData().getIdentifier());}return false;}@Overridepublic String toString() {return "{" + "data:" + data.toString() + "," + "延时时间:" +expire+ DateUtil.format(new Date(),"yyyy.MM.dd HH:mm:ss") + "}";}@Overridepublic long getDelay(TimeUnit unit) {return unit.convert(this.expire - System.currentTimeMillis(), unit);}@Overridepublic int compareTo(Delayed o) {long delta = getDelay(TimeUnit.NANOSECONDS) - o.getDelay(TimeUnit.NANOSECONDS);return (int) delta;}
}
** 任务管理器:**
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Executors;@Component
@Slf4j
public class DelayQueueManager implements CommandLineRunner {private final DelayQueue<DelayTask> delayQueue = new DelayQueue<>();private final Map<String, DelayTask> elements = new HashMap<>();/*** 加入到延时队列中** @param task*/public void put(DelayTask task) {log.error("加入延时任务:{}", task);delayQueue.put(task);}/*** 查询延时任务* @param taskID* @return*/public DelayTask query(String taskID) {return elements.get(taskID);}/*** 取消延时任务** @param task* @return*/public boolean remove(DelayTask task) {log.error("取消延时任务:{}", task);return delayQueue.remove(task);}/*** 取消延时任务** @param taskid* @return*/public boolean remove(String taskid) {return remove(new DelayTask(new TaskBase(taskid), 0));}@Overridepublic void run(String... args) throws Exception {log.info("初始化延时队列");Executors.newSingleThreadExecutor().execute(new Thread(this::excuteThread));}/*** 延时任务执行线程*/private void excuteThread() {while (true) {try {DelayTask task = delayQueue.take();//执行任务processTask(task);} catch (InterruptedException e) {break;}}}/*** 内部执行延时任务** @param task*/private void processTask(DelayTask task) {//获取任务参数,执行业务task.getData().getIdentifier()log.error("执行延时任务:{}-{}", task, task.getData().getIdentifier());}
}
2.3 进行测试
import com.example.demo.task.DelayQueueManager;
import com.example.demo.task.DelayTask;
import com.example.demo.task.TaskBase;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class DemoApplicationTests {@Autowiredprivate DelayQueueManager delayQueueManager;@Testvoid contextLoads() throws InterruptedException {//新增任务delayQueueManager.put(new DelayTask(new TaskBase("abc"), 1000 * 1));//新增任务delayQueueManager.put(new DelayTask(new TaskBase("abc"), 1000 * 5));//新增任务delayQueueManager.put(new DelayTask(new TaskBase("abc"), 1000 * 6));//测试任务需要下边代码执行,线上不用Thread.sleep(10 * 1000);}}
参考链接: springboot延时任务
这篇关于使用DelayQueue的实现延时任务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!