Java Spring Scheduler 动态更改 cron 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20546403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Scheduler change cron expression dynamically
提问by Ahmet Karakaya
I am able to create a taskScheduler in applicationContext.xml, and my job is triggered periodically based on the cron
attribute.
我能够在 applicationContext.xml 中创建一个 taskScheduler,并且我的工作会根据该cron
属性定期触发。
I would like to change this cron
expression(triggering period) after scheduler start, I mean while JavaEE application is running.
我想cron
在调度程序启动后更改此表达式(触发周期),我的意思是在 JavaEE 应用程序运行时。
using Spring 3.XX
使用 Spring 3.XX
采纳答案by Prafful Nagpal
Actually I've faced same issue
其实我遇到了同样的问题
I am assuming you will need to get date(1-31) , time, day of week ,type of scheduler (Daily , monthly , weeekly ) from user.
我假设您需要从用户那里获取日期(1-31)、时间、星期几、调度程序类型(每日、每月、每周)。
First, you need to create cron expression from the given date time from user Following code will create cron expression it takes an map and return cron expression as string.
首先,您需要从用户的给定日期时间创建 cron 表达式 以下代码将创建 cron 表达式,它需要一个映射并将 cron 表达式作为字符串返回。
public String getCronExp(final Map<String, Object> configMap) {
LOGGER.debug(">> getCronExp");
String exp = "";
final String type = (String) configMap.get(SCHEDULER_TYPE);
final String time = (String) configMap.get(TIME);
final String[] split = time.split(this.COLON);
String hour = split[0];
String min = split[1];
if ("00".equalsIgnoreCase(min)) {
min = ZERO;
}
if ("00".equalsIgnoreCase(hour)) {
hour = "0";
}
if ("daily".equalsIgnoreCase(type)) {
exp = this.ZERO + this.WHITE_SPACE + min + this.WHITE_SPACE + hour + this.WHITE_SPACE + this.ASTERISK + this.WHITE_SPACE + this.ASTERISK
+ this.WHITE_SPACE + "?";
} else if ("monthly".equalsIgnoreCase(type)) {
final String date = (String) configMap.get(START_DATE);
exp = this.ZERO + this.WHITE_SPACE + min + this.WHITE_SPACE + hour + this.WHITE_SPACE + date + this.WHITE_SPACE + this.ASTERISK + this.WHITE_SPACE
+ "?";
} else if ("weekly".equalsIgnoreCase(type)) {
final String dayOfWeek = (String) configMap.get(DAY_OF_WEEK);
exp = this.ZERO + this.WHITE_SPACE + min + this.WHITE_SPACE + hour + this.WHITE_SPACE + "?" + this.WHITE_SPACE + this.ASTERISK + this.WHITE_SPACE
+ dayOfWeek;
}
LOGGER.info("Latest cron expression scheduler " + exp);
LOGGER.debug("<< getCronExp");
return exp;
}
After we get cron expression we have issue of triggering the scheduler.
在我们获得 cron 表达式后,我们遇到了触发调度程序的问题。
Create a class that extends runnable:
创建一个扩展 runnable 的类:
public class Scheduler implements Runnable {
@SuppressWarnings("rawtypes")
ScheduledFuture scheduledFuture;
TaskScheduler taskScheduler ;
//this method will kill previous scheduler if exists and will create a new scheduler with given cron expression
public void reSchedule(String cronExpressionStr){
if(taskScheduler== null){
this.taskScheduler = new ConcurrentTaskScheduler();
}
if (this.scheduledFuture() != null) {
this.scheduledFuture().cancel(true);
}
this.scheduledFuture = this.taskScheduler.schedule(this, new CronTrigger(cronExpressionStr));
}
@Override
public void run(){
// task to be performed
}
//if you want on application to read data on startup from db and schedule the schduler use following method
@PostConstruct
public void initializeScheduler() {
//@postcontruct method will be called after creating all beans in application context
// read user config map from db
// get cron expression created
this.reSchedule(cronExp);
}
}