How to reload quartz jobs gracefully

1. The problem

Sometimes, we want to reload quartz jobs periodically, but there is no tutorial on how to do this, now you can follow me to do this job.

2. Environments

  • JDK 1.8
  • Quartz 2.2.1
  • Springboot 1.5.9.RELEASE

3. The solution

First, you should clear the scheduler’s old jobs like this:


then start reloading the jobs

	private void startJob(Scheduler scheduler, JobDef job, boolean reload) throws Exception { //1
		JobDetail jobDetail = newJob(xxx)
					.withIdentity(job.getName(), "group")
					.usingJobData(new JobDataMap(job.getParams())).build();
		TriggerKey triggerKey = new TriggerKey("triggerFor" + job.getName(), "group"); //2
		Trigger trigger = newTrigger().withIdentity(triggerKey)
		if(!reload) { //3
			scheduler.scheduleJob(jobDetail, trigger);
		}else { //4


  • line 1: add a reload parameter to the method, if true, we should reload the job
  • line 2: Before we create a trigger, we instantiate a TriggerKey object , this would be used as the key to reload the job
  • line 3: if reload is false, then we call the scheduler.scheduleJob to schedule the job normally
  • line 4: if reload is true, then we call the scheduler.rescheduleJob using the triggerKey and new trigger info

To summarize, you should not shutdown the scheduler, the scheduler can be reused and reuse the same triggerKey to re-schedule the job.