mk1990
2022-05-16 e6790d88fc5b1ac33d239c0d90ce6cdea666b821
src/main/java/com/genersoft/iot/vmp/conf/DynamicTask.java
@@ -1,7 +1,6 @@
package com.genersoft.iot.vmp.conf;
import com.genersoft.iot.vmp.gb28181.task.ISubscribeTask;
import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.cmd.CatalogResponseMessageHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -9,25 +8,27 @@
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.time.Instant;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
/**
 * 动态定时任务
 * @author lin
 */
@Component
public class DynamicTask {
    private Logger logger = LoggerFactory.getLogger(DynamicTask.class);
    private final Logger logger = LoggerFactory.getLogger(DynamicTask.class);
    @Autowired
    private ThreadPoolTaskScheduler threadPoolTaskScheduler;
    private Map<String, ScheduledFuture<?>> futureMap = new ConcurrentHashMap<>();
    private Map<String, Runnable> runnableMap = new ConcurrentHashMap<>();
    private final Map<String, ScheduledFuture<?>> futureMap = new ConcurrentHashMap<>();
    private final Map<String, Runnable> runnableMap = new ConcurrentHashMap<>();
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
@@ -47,7 +48,7 @@
     * @return
     */
    public void startCron(String key, Runnable task, int cycleForCatalog) {
        ScheduledFuture future = futureMap.get(key);
        ScheduledFuture<?> future = futureMap.get(key);
        if (future != null) {
            if (future.isCancelled()) {
                logger.debug("任务【{}】已存在但是关闭状态!!!", key);
@@ -76,7 +77,9 @@
     */
    public void startDelay(String key, Runnable task, int delay) {
        stop(key);
        Date starTime = new Date(System.currentTimeMillis() + delay);
        // 获取执行的时刻
        Instant startInstant = Instant.now().plusMillis(TimeUnit.MILLISECONDS.toMillis(delay));
        ScheduledFuture future = futureMap.get(key);
        if (future != null) {
@@ -88,7 +91,7 @@
            }
        }
        // scheduleWithFixedDelay 必须等待上一个任务结束才开始计时period, cycleForCatalog表示执行的间隔
        future = threadPoolTaskScheduler.schedule(task, starTime);
        future = threadPoolTaskScheduler.schedule(task, startInstant);
        if (future != null){
            futureMap.put(key, future);
            runnableMap.put(key, task);