'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 1:[1]
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)
.
Solution 2:[2]
With non durable jobs, you must use scheduler.addJob(job, true, true)
instead, with 3 parameters. The third parameter tells Quartz to store the job (in RAM) until it gets scheduled, that is until you add a trigger for it:
void addJob(JobDetail jobDetail, boolean replace, boolean storeNonDurableWhileAwaitingScheduling) throws SchedulerException
With the
storeNonDurableWhileAwaitingScheduling
parameter set totrue
, a non-durable job can be stored. Once it is scheduled, it will resume normal non-durable behavior (i.e. be deleted once there are no remaining associated triggers).
Solution 3:[3]
In addition to the job-specific settings, if you are using Spring you should also make sure that your SchedulerFactoryBean
has overwriteExistingjobs = true
. Otherwise only the initial trigger/settings of a job will be persisted (if you are persisting them).
Solution 4:[4]
This will help to bypass traditional way of updating job.
Delete existing job first using scheduler.deleteJob(jobKey(name,group)).
Then Schedule new job using scheduler.scheduleJob(jobDetail,trigger,true).
surround your code with try/catch(SchedulerEception se)
NOTE: Add this property in quartz properties :) spring.quartz.overwrite-existing-jobs = true or else scheduler will add new instance of a job
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | walen |
Solution 3 | Igor |
Solution 4 | Ajeeth |