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...");}}

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

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

  1. 在定时任务类上增加@EnableScheduling注解,并实现SchedulingConfigurer接口。(值得注意的是:@EnableScheduling对Spring的版本要求比较高,一开始使用的3.2.6版本时一直未成功,后来改成4.2.5版本就可以了)
  2. 设置一个静态变量cron,用于存放任务执行周期参数。
  3. 另辟一线程,用于模拟实际业务中外部原因修改了任务执行周期。
  4. 设置任务触发器,触发任务执行,其中就可以修改任务的执行周期。

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

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() {@Overridepublic void run() {try {Thread.sleep(15 * 1000);} catch (InterruptedException e) {e.printStackTrace();}cron = "0/10 * * * * ?";System.err.println("cron change to: " + cron);}}).start();}@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.addTriggerTask(new Runnable() {@Overridepublic void run() {// 任务逻辑logger.debug("dynamicCronTask is running...");}}, new Trigger() {@Overridepublic Date nextExecutionTime(TriggerContext triggerContext) {// 任务触发,可修改任务的执行周期CronTrigger trigger = new CronTrigger(cron);Date nextExec = trigger.nextExecutionTime(triggerContext);return nextExec;}});}
}

  

Spring @Scheduled定时任务动态修改cron参数相关推荐

  1. java修改动态视频,直播视频app源码,动态修改cron

    直播视频app源码,动态修改cron相关的代码 package com.chashiyu.task.dynamic; import org.springframework.beans.factory. ...

  2. Java Spring @Scheduled 定时任务crontab表达式设置

    Java Spring @Scheduled 定时任务crontab表达式设置 1. Cron详解 2. 例子 参考 1. Cron详解 Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或 ...

  3. vue 动态修改路由参数

    转载自  vue 动态修改路由参数 import merge from 'webpack-merge':修改原有参数 this.$router.push({query:merge(this.$rout ...

  4. Simulink如何在线修改模块参数 (动态修改模块参数)

    文章目录 1. 原由 2. 方法 3. DCP模块 4. 使用 5. 例程 5.1 DCP模块实现可变增益 5.2 DCP模块实现正弦扫频信号 5.3 DCP模块实现时变传递函数 5.4 DCP模块实 ...

  5. Spring Boot 2动态修改日志级别

    本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 作为程序猿,定位问题是我们的日常工作,而日志是我们定位问题非常重要的依据.传统方式定位问题时,往往是如下 ...

  6. SpringBoot实战(十三):Spring Boot Admin 动态修改日志级别

    强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan [前言] 之前关于线上输出日志一直有个困惑:如何可以动态调整的日志级别,来保证系统在正常运行时性 ...

  7. 华为手机使用应用沙盒动态修改分辨率参数

    之前文章介绍了怎么样在安卓手机上安装激活XPOSED框架,XPOSED框架的牛逼之处功能大伙都清楚,可以不修改apk的前提下,修改系统底层的参数,打比方在某些应用范畴,大伙需要修改手机的某个系统参数, ...

  8. vue 替换路由地址参数(动态修改路由参数)

    1.此种方式适用于直接替换已存在的路由参数值,无需新增或删除路由参数: 在项目中,比如进入我的订单页面,订单页面有不同的几个菜单,如:待付款.待发货.待收货.待收货等: 我们想要实现从不同的菜单点击进 ...

  9. 小米手机使用应用沙盒动态修改序列号参数

    较早前文章介绍了怎么在安卓手机上安装激活xposed框架,xposed框架的强大功能大家都清楚,可以不修改APK的前提下,修改系统内部的参数,打比方在某些应用需要,大家需要修改手机的某个系统参数,这个 ...

最新文章

  1. c3074 无法使用带圆括号的_助力带分类简介
  2. 使用 Code Snippet 简化 Coding
  3. TCP服务器和客户端的链接例子(侧重点在注意关闭套接子,减少套接子的描述子)
  4. js如何改变HTML属性,javascript – 如何动态设置HTML lang属性?
  5. 一般能达到多少_实话实说:一般家庭存款有多少?你又达到标准了吗?
  6. ckeditor上传图片文件,研究了一天,终于...
  7. man iptables by iptables-save v1.3.5
  8. dev项目属性按钮是灰色_如何当按钮处于各种交互状态时具有不同样式的按钮?...
  9. python学到什么程度可以找到工作-Python学到什么程度可以面试工作?
  10. 人们为什么使用计算机,人们为什么要用互联网
  11. CRS-1714:Unable to discover any voting files
  12. 超级硬盘数据恢复软件 4.6.5.0注冊码破解版
  13. bilibili 韩顺平Java后端学习路线
  14. 《你不知道的JavaScript》学习佛系梳理
  15. ORB-SLAM3论文翻译
  16. AD域控服务器问题解决记录--lsass.exe流量异常
  17. 我的文档 属性设置里找不到位置选项,以及目录迁移解决方案
  18. 给苹果电脑选机械键盘
  19. 【sdx62】PBL阶段修改GPIO操作
  20. 实现对 2:3 或者3:2的图片进行1:1裁剪

热门文章

  1. 【电气专业知识问答】问:充电器设备的故障如何判断与处理?
  2. CAD中线宽问题的说明
  3. Linux学习笔记05、CentOS 7的中文输入法设置
  4. TP框架引入第三方无命名空间类
  5. 【操作系统】第八章——进程调度算法
  6. nginx-photon升级到2.3.1
  7. photon Unity RPC 调用流程
  8. 解决XLUA example.Hotfix 中报错“please install the Tools”的问题
  9. Android动画浅谈(一)
  10. 多渠道打包和apk加密可以选用的工具