spring boot task實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建定時(shí)任務(wù)

在Spring Boot項(xiàng)目中,通過@EnableScheduling可啟用Spring自帶的定時(shí)任務(wù)支持,在通過@Scheduled注解定義定時(shí)任務(wù),但是通過注解只能編寫固定時(shí)間的定時(shí)任務(wù),無(wú)法動(dòng)態(tài)調(diào)整定時(shí)間隔,可通過實(shí)現(xiàn)SchedulingConfigurer接口實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)注冊(cè)。

參考代碼:

package org.cent.demo.scanner.schedule;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.Arrays;
import java.util.List;

/**
 * @author: cent
 * @email: 292462859@qq.com
 * @date: 2019/1/16.
 * @description:
 */
@Configuration
@EnableScheduling
@Slf4j
public class DynamicSchedule implements SchedulingConfigurer {

    /**
     * 測(cè)試數(shù)據(jù),實(shí)際可從數(shù)據(jù)庫(kù)獲取
     */
    private List<Task> tasks = Arrays.asList(
            new Task(1, "任務(wù)1", "*/30 * * * * *"),
            new Task(2, "任務(wù)2", "*/30 * * * * *"),
            new Task(3, "任務(wù)3", "*/30 * * * * *"),
            new Task(4, "任務(wù)4", "*/30 * * * * *"),
            new Task(5, "任務(wù)5", "*/30 * * * * *"),
            new Task(6, "任務(wù)6", "*/30 * * * * *"),
            new Task(7, "任務(wù)7", "*/30 * * * * *"),
            new Task(8, "任務(wù)8", "*/30 * * * * *"),
            new Task(9, "任務(wù)9", "*/30 * * * * *"),
            new Task(10, "任務(wù)10", "*/30 * * * * *")
    );

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        tasks.forEach(task -> {
            //任務(wù)執(zhí)行線程
            Runnable runnable = () -> log.info("execute task {}", task.getId());

            //任務(wù)觸發(fā)器
            Trigger trigger = triggerContext -> {
                //獲取定時(shí)觸發(fā)器,這里可以每次從數(shù)據(jù)庫(kù)獲取最新記錄,更新觸發(fā)器,實(shí)現(xiàn)定時(shí)間隔的動(dòng)態(tài)調(diào)整
                CronTrigger cronTrigger = new CronTrigger(task.getCron());
                return cronTrigger.nextExecutionTime(triggerContext);
            };

            //注冊(cè)任務(wù)
            scheduledTaskRegistrar.addTriggerTask(runnable, trigger);
        });

    }

    @Data
    @AllArgsConstructor
    static class Task {
        /**
         * 主鍵ID
         */
        private int id;
        /**
         * 任務(wù)名稱
         */
        private String name;
        /**
         * cron表達(dá)式
         */
        private String cron;
    }
}


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容