Spring @Scheduled定時任務動態修改cron參數


  1. 在定時任務類上增加@EnableScheduling注解,並實現SchedulingConfigurer接口。(注意低版本無效)
  2. 設置一個靜態變量cron,用於存放任務執行周期參數。
  3. 另辟一線程,用於模擬實際業務中外部原因修改了任務執行周期。
  4. 設置任務觸發器,觸發任務執行,其中就可以修改任務的執行周期。

Class : SpringDynamicCornTask

package com.xindatai.ibs.lime.dycSchedul;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
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 org.springframework.stereotype.Component;

/**
 * Spring動態周期定時任務 在不停應用的情況下更改任務執行周期
 * 
 * @author Liang
 *
 *         2017年6月1日
 */
@Lazy(false)
@Component
@EnableScheduling
public class SpringDynamicCornTask implements SchedulingConfigurer {

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

    private static String cron;

    private SpringDynamicCornTask() {
        cron = "0/5 * * * * ?";
        // 開啟新線程模擬外部更改了任務執行周期
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(15 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                cron = "0/10 * * * * ?";
                System.out.println("cron change to : " + cron);
            }
        }).start();
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(new Runnable() {

            @Override
            public void run() {
                // 任務邏輯
                logger.info("dynamicCronTask is running...");
            }
        }, new Trigger() {

            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 任務觸發,可修改任務的執行周期
                CronTrigger trigger = new CronTrigger(cron);
                Date nextExecutionTime = trigger.nextExecutionTime(triggerContext);
                return nextExecutionTime;
            }
        });
    } 
}

Console : 

[INFO  2017-06-01 12:26:25 SpringDynamicCornTask] - dynamicCronTask is running...
[INFO  2017-06-01 12:26:30 SpringDynamicCornTask] - dynamicCronTask is running...
[INFO  2017-06-01 12:26:35 SpringDynamicCornTask] - dynamicCronTask is running...
cron change to : 0/10 * * * * ?
[INFO  2017-06-01 12:26:40 SpringDynamicCornTask] - dynamicCronTask is running...
[INFO  2017-06-01 12:26:50 SpringDynamicCornTask] - dynamicCronTask is running...
[INFO  2017-06-01 12:27:00 SpringDynamicCornTask] - dynamicCronTask is running...

啦啦啦

 

啦啦啦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM