spring-bootquartz

Updating a QuartzJob from the running job itself


The update of a QuartzJob within a spring boot application works while the job is not running (here or here). The spring variable spring.quartz.overwrite-existing-jobs: true is set.

However, when doing the same from within a running job the job keeps firing itself in an endless loop without taking into account the interval time (each few milliseconds it fires again). I even tried doing the same from within a TriggerListener but that doesn't change it.

As code example I would have nothing else but what is given in the second link above:

// retrieve the trigger
Trigger oldTrigger = sched.getTrigger(triggerKey("oldTrigger", "group1");

// obtain a builder that would produce the trigger
TriggerBuilder tb = oldTrigger.getTriggerBuilder();

// update the schedule associated with the builder, and build the new trigger
// (other builder methods could be called, to change the trigger in any desired way)
Trigger newTrigger = tb.withSchedule(simpleSchedule()
    .withIntervalInSeconds(10)
    .withRepeatCount(10)
    .build();

sched.rescheduleJob(oldTrigger.getKey(), newTrigger);

Did anyone try that from within a running job?


Solution

  • It works with the following trigger. It is the startAt which makes the difference. Without that the trigger fires immediately again.

    Trigger trigger = newTrigger()
    .withIdentity(triggerName, groupname)
    .startAt(Date.from(LocalDateTime.now().plusSeconds(intervalInSeconds).atZone(ZoneId.systemDefault()).toInstant()))
    .withSchedule(SimpleScheduleBuilder.simpleSchedule()
        .withIntervalInSeconds(intervalInSeconds)
        .repeatForever()
        .withMisfireHandlingInstructionIgnoreMisfires())
        .build();