Python 官方文档:入门教程 => 点击学习
目录spring Task 动态修改任务执行计划cron原理Demo如下Spring @Scheduled定时任务动态修改cron参数Spring Task 动态修改任务执行计划cr
Spring Task 能够在不重启服务的情况下,动态修改批量任务执行时间。
Spring Task目前仅支持TriggerContext上修改下次执行时间(批量任务执行后回调SchedulinGConfigurer.configureTasks,让用户可以重新设置Trigger,从而动态修改下次执行时间)),这种方法的弊端就是“执行时间不能实时生效”。
为此,看了下task文档,感觉若要实现实时生效功能,必须代码里手工启动/停止Spring task任务。
首先,xml里不需要配置springTask相关的executor和scheduler。其次,在代码里自定义scheduler和taskReGIStrar(SpringTask启动的类)。最后,开个线程,模拟动态修改cron表达式的接口。
输出如下:(35min没有执行原task)
INFO 27-11 14:34:10,476 - Initializing ExecutorService
INFO 27-11 14:34:10,484 - nextExecutionTime: 0 35 14 * * ?
修改cron为: 0 36 14 * * ?
INFO 27-11 14:34:20,487 - Initializing ExecutorService
INFO 27-11 14:34:20,488 - nextExecutionTime: 0 36 14 * * ?
INFO 27-11 14:36:00,001 - dynamicCronTask is running...
INFO 27-11 14:36:00,001 - nextExecutionTime: 0 36 14 * * ?
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;
@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...
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Spring Task 动态修改任务执行计划cron方式
本文链接: https://lsjlt.com/news/156131.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0