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;
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Spring @Scheduled定时任务动态修改cron参数
共29个文件
xml:10个
class:5个
prefs:3个
4星 · 超过85%的资源 需积分: 48 463 下载量 199 浏览量
2016-11-10
22:40:58
上传
评论 4
收藏 17KB RAR 举报
温馨提示
在不停服务的情况下,动态修改Spring定时任务的执行周期,即动态修改定时任务的cron参数。
资源推荐
资源详情
资源评论
收起资源包目录
mvc-task-dynamic-cron.rar (29个子文件)
pom.xml 5KB
target
test-classes
log4j.xml 1KB
m2e-wtp
web-resources
META-INF
maven
com.pes_soft
mvc-task-dynamic-cron
pom.xml 5KB
pom.properties 289B
MANIFEST.MF 108B
classes
com
pes_soft
task
demo
SpringDynamicCronTask$3.class 1KB
SpringDynamicCronTask$1.class 1KB
SpringDynamicCronTask$2.class 920B
SpringDynamicCronTask.class 2KB
SpringStaticCronTask.class 973B
log4j.xml 1KB
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.common.component 704B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.validation.prefs 50B
.jsdtscope 575B
org.eclipse.jdt.core.prefs 430B
src
test
resources
log4j.xml 1KB
main
webapp
WEB-INF
web.xml 1KB
views
home.jsp 252B
spring
appServlet
servlet-context.xml 1KB
root-context.xml 930B
resources
log4j.xml 1KB
java
com
pes_soft
task
demo
SpringDynamicCronTask.java 2KB
SpringStaticCronTask.java 641B
.project 1KB
.classpath 1KB
共 29 条
- 1
记录的习惯
- 粉丝: 138
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页