Spring框架自3.0版本起,自帶了任務調度功能,好比是一個輕量級的Quartz,而且使用起來也方便、簡單,且不需要依賴其他的JAR包。秉承着Spring的一貫風格,Spring任務調度的實現同時支持註解配置和XML配置兩種方式。
再來談談變態的項目需求:我們正在做一個智能數字電錶的數據採集項目,項目最終會在多個工業園上線,每個工業園對電錶數據的採集週期可以進行自定義,例如A工業園想每10分鐘採集一次數據,B工業園想每15分鐘採集一次數據。因爲數據採集是個重複的週期性工作,那麼就可以考慮使用Spring框架的定時任務功能了。
按正常來講,修改定時任務的執行週期還不簡單,把服務停下來,改下任務的cron參數,再重啓服務就搞定了。但有沒有一種可能,在不停服務的情況下,就可以動態的修改任務的cron參數呢?完全是有可能的!
先來看下Spring常規定時任務的配置,如下:
- <?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
- 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代碼如下:
- 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秒,期間服務並未停止。