Search code examples
javaquartz-scheduler

How do update a scheduled quartz Job?


Both of the below snippets give this error:

org.quartz.SchedulerException: Jobs added with no trigger must be durable.

  JobDetail job = scheduler.getJobDetail(jobKey(jobInfo));
  job.getJobDataMap().put(JOB_CONTENT, objectMapper.writeValueAsString(jobInfo));
  scheduler.addJob(job, true);


  JobDetail job = JobBuilder
        .newJob(MyJob.class)
        .usingJobData(JOB_CONTENT, objectMapper.writeValueAsString(jobInfo))
        .withIdentity(jobKey(jobInfo))
        .build();
  scheduler.addJob(job, true);

Solution

  • addJob() is for adding jobs with no triggers attached: if that is what you want, just add a call to storeDurably() to the JobBuilder; if, as I can only guess, you want to otherwise update the job while retaining the old trigger, you will need to retrieve the existing trigger first, then, if the trigger will not need changes, scheduler.scheduleJob(newJob, oldTrigger); otherwise get a builder for it using TriggerBuilder.getTriggerBuilder(); to build a copy, make your changes, and eventually call scheduler.scheduleJob(newJob, newTrigger).