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. 根据时间获取最新数据 SQL(每一个人或者每一项)

    -- 方法1 select a.* from table1 a from table1 b where b.name=a.name and b.gdtime>a.gdtime) -- 方法2 s ...

  2. VS2008中设置字体大小和添加显示行号

    1.字体设置 菜单->工具->选项->环境->字体和颜色 在字体那一栏可以选字体,VC6.0默认的字体是Fixedsys,字体大小你可以设为12,就与VC6.0的一样的字体了. ...

  3. oracle中merge into用法解析

    merge into的形式: MERGE INTO [target-table] A USING [source-table sql] B ON([conditional expression] an ...

  4. 解决Eclipse闪退问题的方法总结

    1.在C:/WINDOWS/system32 系统文件夹中ctrl+F 然后搜索java.exe,如果存在java.exe, javaw.exe etc.全部删除. 2.内存不足,打开Eclipse目 ...

  5. [JS] 动态修改ckPlayer播放器宽度

    //设置播放器宽度var play_width=0;$(function(){ play_width = $(window).width() - $(".stu-video-r") ...

  6. c++ 文件增加#ifndef、#define 和 #endif 语句的意义

    文件currency.h (或 currencyOverload.h) 包含了 currency类的声明和实现细节. 在文件头, 应该加上语句 #ifndef Currency_ #define Cu ...

  7. Unity如何内置Visual Studio

    一.问题的起源 软件环境:Unity 2017.3.0f3,Visual Studio 2013 问题描述:在Unity中创建C#脚本后,准备双击打开进行编辑时,出现了Fatal Error. 二.问 ...

  8. Java如何格式化24小时格式的时间?

    在Java中,如何格式化24小时格式的时间?? 此示例使用SimpleDateFormat类的sdf.format(date)方法将时间格式化为24小时格式(00:00-24:00). package ...

  9. Android Handler机制 (一个Thead中可以建立多个Hander,通过msg.target保证MessageQueue中的每个msg交由发送message的handler进行处理 ,但是 每个线程中最多只有一个Looper,肯定也就一个MessageQuque)

    转载自http://blog.csdn.net/stonecao/article/details/6417364 在android中提供了一种异步回调机制Handler,使用它,我们可以在完成一个很长 ...

  10. window wlan 相关服务

    1.Extensible authentication protocol 2.cng key lsolation 3.wlan autoconfig