SpringBoot動態管理定時任務的實現代碼

來源:互聯網
上載者:User
本篇文章給大家帶來的內容是關於SpringBoot動態管理定時任務的實現代碼,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。

SpringBoot動態管理定時任務

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

package com.fighting;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication@EnableSchedulingpublic class ScheduledApplication {    public static void main(String[] args) {        SpringApplication.run(ScheduledApplication.class, args);    }}

配置記錄層級

logging.level.com= debuglogging.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 */@Componentpublic 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 */@Componentpublic 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...
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.