1. 在定时任务类上增加@EnableScheduling注解,并实现SchedulingConfigurer接口。(注意低版本无效)
  2. 设置一个静态变量cron,用于存放任务执行周期参数。
  3. 另辟一线程,用于模拟实际业务中外部原因修改了任务执行周期。
  4. 设置任务触发器,触发任务执行,其中就可以修改任务的执行周期。

Class : SpringDynamicCornTask

package com.xindatai.ibs.lime.dycSchedul;

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动态周期定时任务 在不停应用的情况下更改任务执行周期
*
* @author Liang
*
* 2017年6月1日
*/
@Lazy(false)
@Component
@EnableScheduling
public class SpringDynamicCornTask implements SchedulingConfigurer { private static final Logger logger = LoggerFactory.getLogger(SpringDynamicCornTask.class); private static String cron; private SpringDynamicCornTask() {
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.out.println("cron change to : " + cron);
}
}).start();
} @Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(new Runnable() { @Override
public void run() {
// 任务逻辑
logger.info("dynamicCronTask is running...");
}
}, new Trigger() { @Override
public Date nextExecutionTime(TriggerContext triggerContext) {
// 任务触发,可修改任务的执行周期
CronTrigger trigger = new CronTrigger(cron);
Date nextExecutionTime = trigger.nextExecutionTime(triggerContext);
return nextExecutionTime;
}
});
}
}

Console :

[INFO  2017-06-01 12:26:25 SpringDynamicCornTask] - dynamicCronTask is running...
[INFO 2017-06-01 12:26:30 SpringDynamicCornTask] - dynamicCronTask is running...
[INFO 2017-06-01 12:26:35 SpringDynamicCornTask] - dynamicCronTask is running...
cron change to : 0/10 * * * * ?
[INFO 2017-06-01 12:26:40 SpringDynamicCornTask] - dynamicCronTask is running...
[INFO 2017-06-01 12:26:50 SpringDynamicCornTask] - dynamicCronTask is running...
[INFO 2017-06-01 12:27:00 SpringDynamicCornTask] - dynamicCronTask is running...

啦啦啦

啦啦啦

Spring @Scheduled定时任务动态修改cron参数的更多相关文章

  1. SpringBoot 定时任务升级篇(动态修改cron参数)

    需求缘起:在发布了<Spring Boot定时任务升级篇>之后得到不少反馈,其中有一个反馈就是如何动态修改cron参数呢?那么我们一起看看具体怎么实现,先看下本节大纲: ()简单方式:修改 ...

  2. Spring Cloud Gateway 动态修改请求参数解决 # URL 编码错误传参问题

    Spring Cloud Gateway 动态修改请求参数解决 # URL 编码错误传参问题 继实现动态修改请求 Body 以及重试带 Body 的请求之后,我们又遇到了一个小问题.最近很多接口,收到 ...

  3. Springboot自带定时任务实现动态配置Cron参数

    同学们,我今天分享一下SpringBoot动态配置Cron参数.场景是这样子的:后台管理界面对定时任务进行管理,可动态修改执行时间,然后保存入库,每次任务执行前从库里查询时间,以达到动态修改Cron参 ...

  4. Springboot定时任务实现动态配置Cron参数(从外部数据库获取)

    https://blog.csdn.net/qq_35992900/article/details/80429245 我们主要讲解它的动态配置使用方法. 在刚开始使用的时候,我们更改一个任务的执行时间 ...

  5. Spring @Scheduled定时任务的fixedRate,fixedDelay,cron执行差异

    import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.sp ...

  6. Spring @Scheduled定时任务的fixedRate,fixedDelay,cron的作用和不同

    一.   三种定时类型. 1.cron  --@Scheduled(cron="0/5 * * * *?") 当时间达到设置的时间会触发事件.上面那个例子会每5秒执行一次. 201 ...

  7. Spring Scheduled定时任务报错 java.lang.IllegalStateException: Encountered invalid @Scheduled method 'xxx': For input string: "2S"

    报错信息如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ding ...

  8. redis动态修改参数配置

    ./redis-cli -h 10.10.10.11 -p 6401   save  # 保存当前快照   # 列出所有当前配置 config get *   # 查看指定配置 config get  ...

  9. Spring Boot 系列教程14-动态修改定时任务cron参数

    动态修改定时任务cron参数 不需要重启应用就可以动态的改变Cron表达式的值 不能使用@Scheduled(cron = "${jobs.cron}")实现 DynamicSch ...

随机推荐

  1. iwebshop 模板手册

    1.面包屑 <div class="position"><span>您当前的位置:</span><a href="{url:}& ...

  2. 对比几种在ROS中常用的几种SLAM算法

    在此因为要总结写一个文档,所以查阅资料,将总结的内容记录下来,欢迎大家指正! 文章将介绍使用的基于机器人操作系统(ROS)框架工作的SLAM算法. 在ROS中提供的五种基于2D激光的SLAM算法分别是 ...

  3. Navi.Soft31.开发工具(含下载地址)

    1系统简介 1.1功能简述 在软件开发过程中,我们需要经常对字符串.文件.数据库操作.有时需要浏览Json格式串,有时需要浏览Xml格式串,有时需要读取txt或excel文件,有时需要对数据库访问.本 ...

  4. jquery学习心得:一个很好的css和js函数调用的例子

    统一目录下的资源结构图: <html><head> <link rel="stylesheet" href="gallery.css&quo ...

  5. Patterns for Asynchronous MVVM Applications: Commands

    Stephen Cleary Download the Code Sample This is the second article in a series on combining async an ...

  6. Simple Path Data Resources that I Add to Every WPF and Silverlight Project

    Here’s a little time saver. I sort of have a routine that I go through when I create a new WPF proje ...

  7. 自然语言交流系统 phxnet团队 创新实训 项目博客 (九)

    项目技术总结: VoiceToText的具体使用方法: 语音转文本部分是调用的科大讯飞的在线语音,它的激发方式是按键,通过按钮触发开启安卓设备的录音,此部分需要在源码中写入关于安卓权限的要求,来调用安 ...

  8. Opengl绘制我们的小屋(四)第三人称漫游

    本节内容是在第一人称漫游上完成的,请先了解上文中第一人称漫游的实现. 这一节讲下第三人称漫游是如何实现,第三人称,简单来说,就是在你后面会跟着一台摄像机顺着你拍摄. 先看一下失败的尝试.这个方法是把人 ...

  9. ios中layer动画和UIView动画代码总结

    kCATransitionFade淡出  kCATransitionMoveIn覆盖原图  kCATransitionPush推出  kCATransitionReveal底部显出来    pageC ...

  10. javascript对数据处理

    数组去重 法一: // 遍历数组,建立新数组,利用indexOf判断是否存在于新数组中,不存在则push到新数组,最后返回新数组 function unique(ar) { var ret = []; ...