Search code examples
javaquartz-scheduler

How to reschedule the job execution interval in Quartz?


I am a bit new to Quartz. Is there a way to update the job execution interval for an already submitted Quartz job? Does this interval get updated immediately? Do you have to start the job once again after rescheduling it?

I found the following link but I don't know which libraries is the code referring to since my quartz jars don't contain some of the classes used in the link. Also, where did the triggerKey method come from? Is this some kind of a static import?

http://www.quartz-scheduler.org/documentation/2.4.0-SNAPSHOT/cookbook/UpdateTrigger.html

I want to update the job execution interval to a very large number in one of my JUnit test cases since I don't want the job to interfere with the state of the class under test. Once the test case completes, I want to reset the the job execution interval to the actual value that will be used in production


Solution

  • You have to reschedule the job by creating a new trigger.

    public void execute(JobExecutionContext context) throws JobExecutionException {
        Trigger newTrigger = what_ever_you_want;
        Trigger oldTrigger = context.getTrigger();
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        scheduler.rescheduleJob(oldTrigger.getKey(), newTrigger);
    }
    

    This will replace the same job with a new trigger fire time.