Spring Scheduler动态更改cron表达式

编程入门 行业动态 更新时间:2024-10-28 18:29:26
本文介绍了Spring Scheduler动态更改cron表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以在applicationContext.xml中创建taskScheduler,并根据 cron 属性定期触发我的作业。

I am able to create a taskScheduler in applicationContext.xml, and my job is triggered periodically based on the cron attribute.

我想在调度程序启动后更改此 cron 表达式(触发周期),我的意思是在JavaEE应用程序运行时。

I would like to change this cron expression(triggering period) after scheduler start, I mean while JavaEE application is running.

使用Spring 3.XX

using Spring 3.XX

推荐答案

实际上我遇到了同样的问题

Actually I've faced same issue

我假设您需要从用户那里获得日期(1-31),时间,星期几,调度程序类型(每日,每月,每周)。

I am assuming you will need to get date(1-31) , time, day of week ,type of scheduler (Daily , monthly , weeekly ) from user.

首先,你需要从用户的给定日期时间创建cron表达式。下面的代码将创建cron表达式,它接受一个map并将cron表达式作为字符串返回。

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.

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; }

在我们得到cron表达式后,我们遇到触发调度程序的问题。

After we get cron expression we have issue of triggering the scheduler.

创建一个扩展runnable的类:

Create a class that extends 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); } }

更多推荐

Spring Scheduler动态更改cron表达式

本文发布于:2023-11-24 08:37:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624497.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   动态   Spring   Scheduler   cron

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!