Spring @Scheduled定時任務動態修改cron引數
Spring框架自3.0版本起,自帶了任務排程功能,好比是一個輕量級的Quartz,而且使用起來也方便、簡單,且不需要依賴其他的JAR包。秉承著Spring的一貫風格,Spring任務排程的實現同時支援註解配置和XML配置兩種方式。
再來談談變態的專案需求:我們正在做一個智慧數字電錶的資料採集專案,專案最終會在多個工業園上線,每個工業園對電錶資料的採集週期可以進行自定義,例如A工業園想每10分鐘採集一次資料,B工業園想每15分鐘採集一次資料。因為資料採集是個重複的週期性工作,那麼就可以考慮使用Spring框架的定時任務功能了。
按正常來講,修改定時任務的執行週期還不簡單,把服務停下來,改下任務的cron引數,再重啟服務就搞定了。但有沒有一種可能,在不停服務的情況下,就可以動態的修改任務的cron引數呢?完全是有可能的!
先來看下Spring常規定時任務的配置,如下:
[html] view plain copy print?- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:task="http://www.springframework.org/schema/task"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
- <context:component-scanbase-package="com.pes_soft.task.demo"/>
- <!-- Spring註解方式配置排程任務 -->
- <task:executorid="executor"pool-size="3"/>
- <task:schedulerid="scheduler"pool-size="3"/>
- <task:annotation-drivenexecutor="executor"scheduler="scheduler"/>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<context:component-scan base-package="com.pes_soft.task.demo" />
<!-- Spring註解方式配置排程任務 -->
<task:executor id="executor" pool-size="3"/>
<task:scheduler id="scheduler" pool-size="3"/>
<task:annotation-driven executor="executor" scheduler="scheduler"/>
</beans>
注意:配置Spring定時任務時,需要在Spring配置檔案的xml頭部加入xmlns:task="http://www.springframework.org/schema/task"和xsi:schemaLocation位置中加入http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
然後是註解式任務邏輯程式碼SpringStaticCronTask.java
[java] view plain copy print?- package com.pes_soft.task.demo;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.context.annotation.Lazy;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- /**
- * Spring靜態週期定時任務
- * @Author 許亮
- * @Create 2016-11-10 16:31:29
- */
- @Lazy(false)
- @Component
- publicclass SpringStaticCronTask {
- privatestaticfinal Logger logger = LoggerFactory.getLogger(SpringStaticCronTask.class);
- @Scheduled(cron="0/5 * * * * ?")
- publicvoid staticCronTask() {
- logger.debug("staticCronTask is running...");
- }
- }
package com.pes_soft.task.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* Spring靜態週期定時任務
* @Author 許亮
* @Create 2016-11-10 16:31:29
*/
@Lazy(false)
@Component
public class SpringStaticCronTask {
private static final Logger logger = LoggerFactory.getLogger(SpringStaticCronTask.class);
@Scheduled(cron="0/5 * * * * ?")
public void staticCronTask() {
logger.debug("staticCronTask is running...");
}
}
上述任務適用於具有固定任務週期的任務,若要修改任務執行週期,只能走“停服務→修改任務執行週期→重啟服務”這條路。
下面來看看可以在不停服務的情況下動態修改任務週期的實現,步驟如下:
- 在定時任務類上增加@EnableScheduling註解,並實現SchedulingConfigurer介面。(值得注意的是:@EnableScheduling對Spring的版本要求比較高,一開始使用的3.2.6版本時一直未成功,後來改成4.2.5版本就可以了)
- 設定一個靜態變數cron,用於存放任務執行週期引數。
- 另闢一執行緒,用於模擬實際業務中外部原因修改了任務執行週期。
- 設定任務觸發器,觸發任務執行,其中就可以修改任務的執行週期。
完整的SpringDynamicCronTask.java程式碼如下:
[java] view plain copy print?- package com.pes_soft.task.demo;
- 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動態週期定時任務<br>
- * 在不停應用的情況下更改任務執行週期
- * @Author 許亮
- * @Create 2016-11-10 16:31:29
- */
- @Lazy(false)
- @Component
- @EnableScheduling
- publicclass SpringDynamicCronTask implements SchedulingConfigurer {
- privatestaticfinal Logger logger = LoggerFactory.getLogger(SpringDynamicCronTask.class);
- privatestatic String cron;
- public SpringDynamicCronTask() {
- cron = "0/5 * * * * ?";
- // 開啟新執行緒模擬外部更改了任務執行週期
- new Thread(new Runnable() {
- @Override
- publicvoid run() {
- try {
- Thread.sleep(15 * 1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- cron = "0/10 * * * * ?";
- System.err.println("cron change to: " + cron);
- }
- }).start();
- }
- @Override
- publicvoid configureTasks(ScheduledTaskRegistrar taskRegistrar) {
- taskRegistrar.addTriggerTask(new Runnable() {
- @Override
- publicvoid run() {
- // 任務邏輯
- logger.debug("dynamicCronTask is running...");
- }
- }, new Trigger() {
- @Override
- public Date nextExecutionTime(TriggerContext triggerContext) {
- // 任務觸發,可修改任務的執行週期
- CronTrigger trigger = new CronTrigger(cron);
- Date nextExec = trigger.nextExecutionTime(triggerContext);
- return nextExec;
- }
- });
- }
- }
package com.pes_soft.task.demo;
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動態週期定時任務<br>
* 在不停應用的情況下更改任務執行週期
* @Author 許亮
* @Create 2016-11-10 16:31:29
*/
@Lazy(false)
@Component
@EnableScheduling
public class SpringDynamicCronTask implements SchedulingConfigurer {
private static final Logger logger = LoggerFactory.getLogger(SpringDynamicCronTask.class);
private static String cron;
public SpringDynamicCronTask() {
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.err.println("cron change to: " + cron);
}
}).start();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() {
@Override
public void run() {
// 任務邏輯
logger.debug("dynamicCronTask is running...");
}
}, new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// 任務觸發,可修改任務的執行週期
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
}
});
}
}
將demo執行起來,檢視任務執行情況,可以觀察到任務的執行週期由5秒變成了10秒,期間服務並未停止。