Need to set the quartz cron expression dynamically

后端 未结 3 1703
盖世英雄少女心
盖世英雄少女心 2020-12-30 13:48

I\'m using quartz in my web application (Servlet web app) following is snap of quartz.property file and the quartz.job.xml

quartz.property

#========         


        
相关标签:
3条回答
  • 2020-12-30 14:25
    CronTrigger cronTrigger = (CronTrigger) stdScheduler
                    .getTrigger(triggerName,triggerGroupName);
    CronTrigger newTriggerIns = new CronTrigger();
     newTriggerIns.setJobName(cronTrigger.getJobName());
     newTriggerIns.setName(triggerName);
     newTriggerIns.setCronExpression(newCronExpression);
     stdScheduler.rescheduleJob(triggerName,triggerGroupName,newTriggerIns);
    

    In the above, take the existing trigger instance. Create one new trigger instance and set cron expression.

    Then reschedule the existing instance with new one.

    0 讨论(0)
  • 2020-12-30 14:44

    Use Quartz api. Programmatically take this trigger instance, cast it to CronTrigger instance and use it's setCronExpression to put expression dynamically.

    0 讨论(0)
  • 2020-12-30 14:47

    Creating a new Trigger like this doesn't work.

    CronTrigger cronTrigger = (CronTrigger) stdScheduler.getTrigger(triggerName,triggerGroupName);
    CronTrigger newTriggerIns = new CronTrigger();
    newTriggerIns.setJobName(cronTrigger.getJobName());
    newTriggerIns.setName(triggerName);
    newTriggerIns.setCronExpression(newCronExpression);
    stdScheduler.rescheduleJob(triggerName,triggerGroupName,newTriggerIns); //doesn't work
    

    You just have to edit the original trigger like this:

    CronTrigger cronTrigger = (CronTrigger) stdScheduler.getTrigger(triggerName,triggerGroupName);
    cronTrigger.setCronExpression(newCronExpression);
    stdScheduler.rescheduleJob(triggerName,triggerGroupName,cronTrigger);
    
    0 讨论(0)
提交回复
热议问题