java使用DelayQueue實現(xiàn)延時任務
1、背景
項目中經(jīng)常會用到類似一些需要延遲執(zhí)行的功能,比如緩存。java提供了DelayQueue來很輕松的實現(xiàn)這種功能。Delayed接口中的getDelay方法返回值小于等于0的時候,表示時間到達,可以從DelayQueue中通過take()方法取的到期的對象。到期對象是實現(xiàn)了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 整體架構
** 工具類:**
執(zhí)行任務所需的基礎參數(shù)
import lombok.Data; @Data public class TaskBase { //任務參數(shù),根據(jù)業(yè)務需求多少都行 private String identifier; public TaskBase(String identifier) { this.identifier = identifier; } }
執(zhí)行的任務和時間
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 { //任務參數(shù) final private TaskBase data; //任務的延時時間,單位毫秒 final private long expire; /** * 構造延時任務 * * @param data 業(yè)務數(shù)據(jù) * @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; } @Override public boolean equals(Object obj) { if (obj instanceof DelayTask) { return this.data.getIdentifier().equals(((DelayTask) obj).getData().getIdentifier()); } return false; } @Override public String toString() { return "{" + "data:" + data.toString() + "," + "延時時間:" +expire+ DateUtil.format(new Date(),"yyyy.MM.dd HH:mm:ss") + "}"; } @Override public long getDelay(TimeUnit unit) { return unit.convert(this.expire - System.currentTimeMillis(), unit); } @Override public 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)); } @Override public void run(String... args) throws Exception { log.info("初始化延時隊列"); Executors.newSingleThreadExecutor().execute(new Thread(this::excuteThread)); } /** * 延時任務執(zhí)行線程 */ private void excuteThread() { while (true) { try { DelayTask task = delayQueue.take(); //執(zhí)行任務 processTask(task); } catch (InterruptedException e) { break; } } } /** * 內(nèi)部執(zhí)行延時任務 * * @param task */ private void processTask(DelayTask task) { //獲取任務參數(shù),執(zhí)行業(yè)務task.getData().getIdentifier() log.error("執(zhí)行延時任務:{}-{}", 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 { @Autowired private DelayQueueManager delayQueueManager; @Test void 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)); //測試任務需要下邊代碼執(zhí)行,線上不用 Thread.sleep(10 * 1000); } }
到此這篇關于java使用DelayQueue實現(xiàn)延時任務的文章就介紹到這了,更多相關DelayQueue延時任務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot結(jié)合Redis實現(xiàn)序列化的方法詳解
Spring提供了一個RedisTemplate來進行對Redis的操作,但是RedisTemplate默認配置的是使用Java本機序列化。如果要對對象操作,就不是那么的方便。所以本文為大家介紹了另一種SpringBoot結(jié)合Redis實現(xiàn)序列化的方法,需要的可以參考一下2022-06-06使用Java橋接模式打破繼承束縛優(yōu)雅實現(xiàn)多維度變化
這篇文章主要為大家介紹了使用Java橋接模式打破繼承束縛,優(yōu)雅實現(xiàn)多維度變化,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05Mybatis plus 配置多數(shù)據(jù)源的實現(xiàn)示例
這篇文章主要介紹了Mybatis plus 配置多數(shù)據(jù)源的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08