Spring @Scheduled定时任务动态修改cron参数

来源:互联网 发布:电脑软件怎么卸载 编辑:程序博客网 时间:2024/06/01 08:58

  Spring框架自3.0版本起,自带了任务调度功能,好比是一个轻量级的Quartz,而且使用起来也方便、简单,且不需要依赖其他的JAR包。秉承着Spring的一贯风格,Spring任务调度的实现同时支持注解配置和XML配置两种方式。

  再来谈谈变态的项目需求:我们正在做一个智能数字电表的数据采集项目,项目最终会在多个工业园上线,每个工业园对电表数据的采集周期可以进行自定义,例如A工业园想每10分钟采集一次数据,B工业园想每15分钟采集一次数据。因为数据采集是个重复的周期性工作,那么就可以考虑使用Spring框架的定时任务功能了。

  按正常来讲,修改定时任务的执行周期还不简单,把服务停下来,改下任务的cron参数,再重启服务就搞定了。但有没有一种可能,在不停服务的情况下,就可以动态的修改任务的cron参数呢?完全是有可能的!

  先来看下Spring常规定时任务的配置,如下:

[html] view plain copy
print?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="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.       
  11.     <context:component-scan base-package="com.pes_soft.task.demo" />  
  12.       
  13.     <!-- Spring注解方式配置调度任务 -->  
  14.     <task:executor id="executor" pool-size="3"/>  
  15.     <task:scheduler id="scheduler" pool-size="3"/>  
  16.     <task:annotation-driven executor="executor" scheduler="scheduler"/>  
  17. </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.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.context.annotation.Lazy;  
  6. import org.springframework.scheduling.annotation.Scheduled;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. /** 
  10.  * Spring静态周期定时任务 
  11.  * @Author 许亮 
  12.  * @Create 2016-11-10 16:31:29 
  13.  */  
  14. @Lazy(false)  
  15. @Component  
  16. public class SpringStaticCronTask {  
  17.     private static final Logger logger = LoggerFactory.getLogger(SpringStaticCronTask.class);  
  18.       
  19.     @Scheduled(cron="0/5 * * * * ?")  
  20.     public void staticCronTask() {  
  21.         logger.debug("staticCronTask is running...");  
  22.     }  
  23.       
  24. }  

  上述任务适用于具有固定任务周期的任务,若要修改任务执行周期,只能走“停服务→修改任务执行周期→重启服务”这条路。

  下面来看看可以在不停服务的情况下动态修改任务周期的实现,步骤如下:

  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.   
  3. import java.util.Date;  
  4.   
  5. import org.slf4j.Logger;  
  6. import org.slf4j.LoggerFactory;  
  7. import org.springframework.context.annotation.Lazy;  
  8. import org.springframework.scheduling.Trigger;  
  9. import org.springframework.scheduling.TriggerContext;  
  10. import org.springframework.scheduling.annotation.EnableScheduling;  
  11. import org.springframework.scheduling.annotation.SchedulingConfigurer;  
  12. import org.springframework.scheduling.config.ScheduledTaskRegistrar;  
  13. import org.springframework.scheduling.support.CronTrigger;  
  14. import org.springframework.stereotype.Component;  
  15.   
  16. /** 
  17.  * Spring动态周期定时任务<br> 
  18.  * 在不停应用的情况下更改任务执行周期 
  19.  * @Author 许亮 
  20.  * @Create 2016-11-10 16:31:29 
  21.  */  
  22. @Lazy(false)  
  23. @Component  
  24. @EnableScheduling  
  25. public class SpringDynamicCronTask implements SchedulingConfigurer {  
  26.     private static final Logger logger = LoggerFactory.getLogger(SpringDynamicCronTask.class);  
  27.       
  28.     private static String cron;  
  29.       
  30.     public SpringDynamicCronTask() {  
  31.         cron = "0/5 * * * * ?";  
  32.           
  33.         // 开启新线程模拟外部更改了任务执行周期  
  34.         new Thread(new Runnable() {  
  35.             @Override  
  36.             public void run() {  
  37.                 try {  
  38.                     Thread.sleep(15 * 1000);  
  39.                 } catch (InterruptedException e) {  
  40.                     e.printStackTrace();  
  41.                 }  
  42.                   
  43.                 cron = "0/10 * * * * ?";  
  44.                 System.err.println("cron change to: " + cron);  
  45.             }  
  46.         }).start();  
  47.     }  
  48.   
  49.     @Override  
  50.     public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {  
  51.         taskRegistrar.addTriggerTask(new Runnable() {  
  52.             @Override  
  53.             public void run() {  
  54.                 // 任务逻辑  
  55.                 logger.debug("dynamicCronTask is running...");  
  56.             }  
  57.         }, new Trigger() {  
  58.             @Override  
  59.             public Date nextExecutionTime(TriggerContext triggerContext) {  
  60.                 // 任务触发,可修改任务的执行周期  
  61.                 CronTrigger trigger = new CronTrigger(cron);  
  62.                 Date nextExec = trigger.nextExecutionTime(triggerContext);  
  63.                 return nextExec;  
  64.             }  
  65.         });  
  66.     }  
  67. }  

  将demo运行起来,查看任务执行情况,可以观察到任务的执行周期由5秒变成了10秒,期间服务并未停止。

  

  源码下载:mvc-task-dynamic-cron.rar