1. 程式人生 > >Spring @Scheduled定時任務動態修改cron引數

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

  Spring框架自3.0版本起,自帶了任務排程功能,好比是一個輕量級的Quartz,而且使用起來也方便、簡單,且不需要依賴其他的JAR包。秉承著Spring的一貫風格,Spring任務排程的實現同時支援註解配置和XML配置兩種方式。

  再來談談變態的專案需求:我們正在做一個智慧數字電錶的資料採集專案,專案最終會在多個工業園上線,每個工業園對電錶資料的採集週期可以進行自定義,例如A工業園想每10分鐘採集一次資料,B工業園想每15分鐘採集一次資料。因為資料採集是個重複的週期性工作,那麼就可以考慮使用Spring框架的定時任務功能了。

  按正常來講,修改定時任務的執行週期還不簡單,把服務停下來,改下任務的cron引數,再重啟服務就搞定了。但有沒有一種可能,在不停服務的情況下,就可以動態的修改任務的cron引數呢?完全是有可能的!

  先來看下Spring常規定時任務的配置,如下:

[html] view plain copy print?
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:task="http://www.springframework.org/schema/task"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
  8.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd   
  9.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
  10.     <context:component-scanbase-package="com.pes_soft.task.demo"/>
  11.     <!-- Spring註解方式配置排程任務 -->
  12.     <task:executorid="executor"pool-size="3"/>
  13.     <task:schedulerid="scheduler"pool-size="3"/>
  14.     <task:annotation-drivenexecutor="executor"scheduler="scheduler"/>
  15. </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?
  1. package com.pes_soft.task.demo;  
  2. import org.slf4j.Logger;  
  3. import org.slf4j.LoggerFactory;  
  4. import org.springframework.context.annotation.Lazy;  
  5. import org.springframework.scheduling.annotation.Scheduled;  
  6. import org.springframework.stereotype.Component;  
  7. /** 
  8.  * Spring靜態週期定時任務 
  9.  * @Author 許亮 
  10.  * @Create 2016-11-10 16:31:29 
  11.  */
  12. @Lazy(false)  
  13. @Component
  14. publicclass SpringStaticCronTask {  
  15.     privatestaticfinal Logger logger = LoggerFactory.getLogger(SpringStaticCronTask.class);  
  16.     @Scheduled(cron="0/5 * * * * ?")  
  17.     publicvoid staticCronTask() {  
  18.         logger.debug("staticCronTask is running...");  
  19.     }  
  20. }  
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...");
	}
	
}

  上述任務適用於具有固定任務週期的任務,若要修改任務執行週期,只能走“停服務→修改任務執行週期→重啟服務”這條路。

  下面來看看可以在不停服務的情況下動態修改任務週期的實現,步驟如下:

  1. 在定時任務類上增加@EnableScheduling註解,並實現SchedulingConfigurer介面。(值得注意的是:@EnableScheduling對Spring的版本要求比較高,一開始使用的3.2.6版本時一直未成功,後來改成4.2.5版本就可以了)
  2. 設定一個靜態變數cron,用於存放任務執行週期引數。
  3. 另闢一執行緒,用於模擬實際業務中外部原因修改了任務執行週期。
  4. 設定任務觸發器,觸發任務執行,其中就可以修改任務的執行週期。

  完整的SpringDynamicCronTask.java程式碼如下:

[java] view plain copy print?
  1. package com.pes_soft.task.demo;  
  2. import java.util.Date;  
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.context.annotation.Lazy;  
  6. import org.springframework.scheduling.Trigger;  
  7. import org.springframework.scheduling.TriggerContext;  
  8. import org.springframework.scheduling.annotation.EnableScheduling;  
  9. import org.springframework.scheduling.annotation.SchedulingConfigurer;  
  10. import org.springframework.scheduling.config.ScheduledTaskRegistrar;  
  11. import org.springframework.scheduling.support.CronTrigger;  
  12. import org.springframework.stereotype.Component;  
  13. /** 
  14.  * Spring動態週期定時任務<br> 
  15.  * 在不停應用的情況下更改任務執行週期 
  16.  * @Author 許亮 
  17.  * @Create 2016-11-10 16:31:29 
  18.  */
  19. @Lazy(false)  
  20. @Component
  21. @EnableScheduling
  22. publicclass SpringDynamicCronTask implements SchedulingConfigurer {  
  23.     privatestaticfinal Logger logger = LoggerFactory.getLogger(SpringDynamicCronTask.class);  
  24.     privatestatic String cron;  
  25.     public SpringDynamicCronTask() {  
  26.         cron = "0/5 * * * * ?";  
  27.         // 開啟新執行緒模擬外部更改了任務執行週期
  28.         new Thread(new Runnable() {  
  29.             @Override
  30.             publicvoid run() {  
  31.                 try {  
  32.                     Thread.sleep(15 * 1000);  
  33.                 } catch (InterruptedException e) {  
  34.                     e.printStackTrace();  
  35.                 }  
  36.                 cron = "0/10 * * * * ?";  
  37.                 System.err.println("cron change to: " + cron);  
  38.             }  
  39.         }).start();  
  40.     }  
  41.     @Override
  42.     publicvoid configureTasks(ScheduledTaskRegistrar taskRegistrar) {  
  43.         taskRegistrar.addTriggerTask(new Runnable() {  
  44.             @Override
  45.             publicvoid run() {  
  46.                 // 任務邏輯
  47.                 logger.debug("dynamicCronTask is running...");  
  48.             }  
  49.         }, new Trigger() {  
  50.             @Override
  51.             public Date nextExecutionTime(TriggerContext triggerContext) {  
  52.                 // 任務觸發,可修改任務的執行週期
  53.                 CronTrigger trigger = new CronTrigger(cron);  
  54.                 Date nextExec = trigger.nextExecutionTime(triggerContext);  
  55.                 return nextExec;  
  56.             }  
  57.         });  
  58.     }  
  59. }  
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秒,期間服務並未停止。