You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.2 KiB
43 lines
1.2 KiB
![]()
4 years ago
|
package com.genersoft.iot.vmp.conf;
|
||
|
|
||
|
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.Map;
|
||
|
import java.util.concurrent.ConcurrentHashMap;
|
||
|
import java.util.concurrent.ScheduledFuture;
|
||
|
|
||
|
/**
|
||
|
* 动态定时任务
|
||
|
*/
|
||
|
@Component
|
||
|
public class DynamicTask {
|
||
|
|
||
|
@Autowired
|
||
|
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
|
||
|
|
||
|
private Map<String, ScheduledFuture<?>> futureMap = new ConcurrentHashMap<>();
|
||
|
|
||
|
@Bean
|
||
|
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
|
||
|
return new ThreadPoolTaskScheduler();
|
||
|
}
|
||
|
|
||
|
public String startCron(String key, Runnable task, String corn) {
|
||
|
stopCron(key);
|
||
|
ScheduledFuture future = threadPoolTaskScheduler.schedule(task, new CronTrigger(corn));
|
||
|
futureMap.put(key, future);
|
||
|
return "startCron";
|
||
|
}
|
||
|
|
||
|
public void stopCron(String key) {
|
||
|
if (futureMap.get(key) != null && !futureMap.get(key).isCancelled()) {
|
||
|
futureMap.get(key).cancel(true);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|