648540858
2022-03-02 1dcdbc3742835ccab28a8983ae002d2bbdba87eb
src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java
@@ -3,7 +3,6 @@
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;
@@ -26,9 +25,10 @@
        return new ThreadPoolTaskScheduler();
    }
    public String startCron(String key, Runnable task, String corn) {
    public String startCron(String key, Runnable task, int cycleForCatalog) {
        stopCron(key);
        ScheduledFuture future = threadPoolTaskScheduler.schedule(task, new CronTrigger(corn));
        // scheduleWithFixedDelay 必须等待上一个任务结束才开始计时period, cycleForCatalog表示执行的间隔
        ScheduledFuture future = threadPoolTaskScheduler.scheduleWithFixedDelay(task, cycleForCatalog * 1000L);
        futureMap.put(key, future);
        return "startCron";
    }
@@ -39,4 +39,8 @@
        }
    }
    public boolean contains(String key) {
        return futureMap.get(key) != null;
    }
}