首頁  >  文章  >  Java  >  SpringBoot動態管理定時任務的實作程式碼

SpringBoot動態管理定時任務的實作程式碼

不言
不言原創
2018-09-11 14:20:584290瀏覽

這篇文章帶給大家的內容是關於SpringBoot動態管理定時任務的實現程式碼,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

SpringBoot動態管理定時任務

首先使用@EnableScheduling 開啟定時任務

package com.fighting;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class ScheduledApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduledApplication.class, args);
    }
}

配置日誌等級

logging.level.com= debug
logging.file=springboot-scheduled.log

固定週期定時任務

package com.fighting;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Spring静态周期定时任务
 *
 * @author fighting
 * @date 2018-09-11
 */
@Component
public class SpringStaticCronTask {

    public static final Logger logger = LoggerFactory.getLogger(SpringStaticCronTask.class);


    @Scheduled(cron = "0/5 * * * * ?")
    public void staticCornTask() {
        logger.debug("staticCronTask is running...");
    }


}

動態修改定時週期

實現SchedulingConfigurer 接口,重寫configureTasks方法

package com.fighting;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

/**
 * 动态定时任务
 *
 * @author fighting
 * @date 2018-09-11
 */
@Component
public class SpringDynamicCronTask implements SchedulingConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(SpringDynamicCronTask.class);

    private static String cron = "0/5 * * * * ?";


    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(() -> {
            // 任务逻辑
            logger.error("dynamicCronTask is running...");
        }, triggerContext -> {
            // 任务触发,在这里可修改任务的执行周期,因为每次调度都会执行这里
            CronTrigger cronTrigger = new CronTrigger(cron);
            return cronTrigger.nextExecutionTime(triggerContext);
        });

    }


    public SpringDynamicCronTask() {
        //模拟业务修改周期,可以在具体业务中修改参数cron
        new Thread(() -> {
            try {
                Thread.sleep(15000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cron = "0/2 * * * * ?";
        }).start();
    }

}

 觀察列印結果,週期變了,但是專案沒有重新啟動。

2018-09-11 13:36:50.009 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:36:50.010 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:36:55.001 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:36:55.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:00.009 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:37:00.016 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:05.016 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:37:05.016 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:06.013 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:08.008 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:10.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:10.003 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:37:12.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:14.006 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:15.015 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask        : staticCronTask is running...
2018-09-11 13:37:16.012 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...
2018-09-11 13:37:18.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask       : dynamicCronTask is running...

相關推薦:

SpringBoot 調度任務及常用任務表達式

透過reidis管理定時任務

以上是SpringBoot動態管理定時任務的實作程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn