调度配置器的使用

调度配置器的使用(SchedulingConfigurer)

解决动态加载cron配置的问题

@Slf4j
@Component
@EnableScheduling
public class DynamicCronTask implements SchedulingConfigurer {
    
    // 这里可以注入config
    private String cron = "";

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                // 任务逻辑
                log.debug("dynamicCronTask is running...");
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 任务触发,可修改任务的执行周期
                CronTrigger trigger = new CronTrigger(cron);
                Date nextExec = trigger.nextExecutionTime(triggerContext);
                return nextExec;
            }
        });
    }
}

主要在于方法addTriggerTask

private List triggerTasks;

public void addTriggerTask(Runnable task, Trigger trigger) {
    this.addTriggerTask(new TriggerTask(task, trigger));
}

public void addTriggerTask(TriggerTask task) {
    if (this.triggerTasks == null) {
        this.triggerTasks = new ArrayList();
    }
    this.triggerTasks.add(task);
}


addTriggerTask-添加执行任务的方法有两个参数,一个是Runnable,一个是Trigger,将这两个参数搞成了一个TriggerTask对象,最终放到了triggerTasks集合当中,
那么我们执行这个有序集合,就可以执行任务了

如何执行任务的

protected void scheduleTasks() {
    if (this.taskScheduler == null) {
        this.localExecutor = Executors.newSingleThreadScheduledExecutor();
        this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
    }

    Iterator var1;
    if (this.triggerTasks != null) {
        var1 = this.triggerTasks.iterator();

        while(var1.hasNext()) {
            TriggerTask task = (TriggerTask)var1.next();
            this.addScheduledTask(this.scheduleTriggerTask(task));
        }
    }

    if (this.cronTasks != null) {
        var1 = this.cronTasks.iterator();

        while(var1.hasNext()) {
            CronTask task = (CronTask)var1.next();
            this.addScheduledTask(this.scheduleCronTask(task));
        }
    }

    IntervalTask task;
    if (this.fixedRateTasks != null) {
        var1 = this.fixedRateTasks.iterator();

        while(var1.hasNext()) {
            task = (IntervalTask)var1.next();
            this.addScheduledTask(this.scheduleFixedRateTask(task));
        }
    }

    if (this.fixedDelayTasks != null) {
        var1 = this.fixedDelayTasks.iterator();

        while(var1.hasNext()) {
            task = (IntervalTask)var1.next();
            this.addScheduledTask(this.scheduleFixedDelayTask(task));
        }
    }

}


执行的队列很多,我们只用到了triggerTasks ,所以我们重点看这些代码

private final Set scheduledTasks = new LinkedHashSet(16);

protected void scheduleTasks() {
    Iterator var1;
    if (this.triggerTasks != null) {
        var1 = this.triggerTasks.iterator();

        while(var1.hasNext()) {
            TriggerTask task = (TriggerTask)var1.next();
            this.addScheduledTask(this.scheduleTriggerTask(task));
        }
    }
}

private void addScheduledTask(@Nullable ScheduledTask task) {
    if (task != null) {
        this.scheduledTasks.add(task);
    }

}

@Nullable
public ScheduledTask scheduleTriggerTask(TriggerTask task) {
    ScheduledTask scheduledTask = (ScheduledTask)this.unresolvedTasks.remove(task);
    boolean newTask = false;
    if (scheduledTask == null) {
        scheduledTask = new ScheduledTask(task);
        newTask = true;
    }

    if (this.taskScheduler != null) {
        scheduledTask.future = this.taskScheduler.schedule(task.getRunnable(), task.getTrigger());
    } else {
        this.addTriggerTask(task);
        this.unresolvedTasks.put(task, scheduledTask);
    }

    return newTask ? scheduledTask : null;
}


将有序集合triggerTasks的任务遍历,使用taskScheduler.schedule方法进行执行任务,将执行完的任务放到scheduledTasks唯一集合中,执行中的任务不可重复。

你可能感兴趣的:(调度配置器的使用)