Spring Scheduler动态更改cron表达式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Scheduler动态更改cron表达式相关的知识,希望对你有一定的参考价值。

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

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

使用Spring 3.XX

答案

其实我遇到过同样的问题

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

首先,您需要从用户的给定日期时间创建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;
}

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

创建一个扩展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表达式的主要内容,如果未能解决你的问题,请参考以下文章

.NET Kendo Scheduler:动态更改数据源资源

利用spring+quartz-scheduler+MySql实现定时任务动态调整

利用spring+quartz-scheduler+MySql实现定时任务动态调整

请让我知道Apache Camel中的民意调查工作机制(CRON JOB)

LTS

Spring Boot集成Spring Scheduler和Quartz Scheduler