From 4abbf4df07f02c0c94baa2787fdcf4a14dcb3463 Mon Sep 17 00:00:00 2001
From: sc.dm <822878110@qq.com>
Date: 星期四, 10 三月 2022 17:42:02 +0800
Subject: [PATCH] Merge branch '648540858:wvp-28181-2.0' into wvp-28181-2.0
---
src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java | 38 +++++++++++++++++++++++++++++++++-----
1 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java b/src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java
index 145c29b..80e39f5 100644
--- a/src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java
+++ b/src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java
@@ -3,9 +3,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
-import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
+import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
@@ -26,17 +26,45 @@
return new ThreadPoolTaskScheduler();
}
- public String startCron(String key, Runnable task, String corn) {
- stopCron(key);
- ScheduledFuture future = threadPoolTaskScheduler.schedule(task, new CronTrigger(corn));
+ /**
+ * 寰幆鎵ц鐨勪换鍔�
+ * @param key 浠诲姟ID
+ * @param task 浠诲姟
+ * @param cycleForCatalog 闂撮殧
+ * @return
+ */
+ public String startCron(String key, Runnable task, int cycleForCatalog) {
+ stop(key);
+ // scheduleWithFixedDelay 蹇呴』绛夊緟涓婁竴涓换鍔$粨鏉熸墠寮�濮嬭鏃秔eriod锛� cycleForCatalog琛ㄧず鎵ц鐨勯棿闅�
+ ScheduledFuture future = threadPoolTaskScheduler.scheduleWithFixedDelay(task, cycleForCatalog * 1000L);
futureMap.put(key, future);
return "startCron";
}
- public void stopCron(String key) {
+ /**
+ * 寤舵椂浠诲姟
+ * @param key 浠诲姟ID
+ * @param task 浠诲姟
+ * @param delay 寤舵椂 /绉�
+ * @return
+ */
+ public String startDelay(String key, Runnable task, int delay) {
+ stop(key);
+ Date starTime = new Date(System.currentTimeMillis() + delay * 1000);
+ // scheduleWithFixedDelay 蹇呴』绛夊緟涓婁竴涓换鍔$粨鏉熸墠寮�濮嬭鏃秔eriod锛� cycleForCatalog琛ㄧず鎵ц鐨勯棿闅�
+ ScheduledFuture future = threadPoolTaskScheduler.schedule(task, starTime);
+ futureMap.put(key, future);
+ return "startCron";
+ }
+
+ public void stop(String key) {
if (futureMap.get(key) != null && !futureMap.get(key).isCancelled()) {
futureMap.get(key).cancel(true);
}
}
+ public boolean contains(String key) {
+ return futureMap.get(key) != null;
+ }
+
}
--
Gitblit v1.8.0