Quartz Scheduler Job Exception Handling Example
November 20, 2023
Here we will walk through the quartz scheduler job exception handling example with JobExecutionException
that has methods such as setRefireImmediately()
, refireImmediately()
and setUnscheduleAllTriggers()
. By calling these methods, we give the signal to quartz whether to refire the job immediately or unschedule all the triggers. We create the instance of JobExecutionException
in catch block by passing the exception instance and throws itself. Here in this page we will provide complete example step by step.
1. JobExecutionException
org.quartz.JobExecutionException is thrown byexecute()
method of org.quartz.Job. By this exception, quartz scheduler comes to know that job execution has failed. While throwing exception, JobExecutionException
can be either set to refire job immediately or unscheduled the current or all triggers. JobExecutionException
is initialized in catch block by passing the exception instance as below.
JobExecutionException jee = new JobExecutionException(ex);
setRefireImmediately() : This accepts Boolean. If we set false, it will not refire the job and if true, the job will refire again and again.
refireImmediately() : By calling this method we set the refire immediately flag false. refireImmediately() and setRefireImmediately(false) behaves in same way.
setUnscheduleAllTriggers() : Setting true, we indicate quartz to un schedule all triggers.
2. Using refireImmediately()
Here we will create a job which will fail because of arithmetical exception and in catch block we will call refireImmediately()
that will call next schedule after the given interval for the given number of times.
MyJobOne.java
package com.concretepage.job; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; public class MyJobOne implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { JobKey jobKey = context.getJobDetail().getKey(); try { //divide by zero int res= 10/0; } catch (ArithmeticException ex) { System.out.println(jobKey+": error occured."); JobExecutionException jee = new JobExecutionException(ex); //jee.setRefireImmediately(false); jee.refireImmediately(); throw jee; } } }
package com.concretepage; import java.util.Calendar; import java.util.Date; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import com.concretepage.job.MyJobOne; public class ExceptionHandlingDemoOne { public static void main(String[] args) throws SchedulerException { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); JobDetail jobDetail = JobBuilder.newJob(MyJobOne.class).withIdentity("job1", "mygroup").build(); SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "mygroup") .startAt(new Date(Calendar.getInstance().getTimeInMillis()+ 2000)) .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2) .withRepeatCount(2)).build(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); try { //wait for 10 seconds to finish the job Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } scheduler.shutdown(true); } }
mygroup.job1: error occured. mygroup.job1: error occured. mygroup.job1: error occured.
3. Using setUnscheduleAllTriggers()
In this example, we are creating a job which will use setUnscheduleAllTriggers(true)
to un schedule all triggers.
MyJobTwo.java
package com.concretepage.job; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; public class MyJobTwo implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { JobKey jobKey = context.getJobDetail().getKey(); try { //divide by zero int res= 10/0; } catch (ArithmeticException ex) { System.out.println(jobKey+": error occured."); JobExecutionException jee = new JobExecutionException(ex); jee.setUnscheduleAllTriggers(true); throw jee; } } }
package com.concretepage; import java.util.Calendar; import java.util.Date; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import com.concretepage.job.MyJobTwo; public class ExceptionHandlingDemoTwo { public static void main(String[] args) throws SchedulerException { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); JobDetail jobDetail = JobBuilder.newJob(MyJobTwo.class).withIdentity("job2", "mygroup").build(); SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger2", "mygroup") .startAt(new Date(Calendar.getInstance().getTimeInMillis()+ 2000)) .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2) .withRepeatCount(3)).build(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); try { //wait for 10 seconds to finish the job Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } //shutdown scheduler gracefully scheduler.shutdown(true); } }
mygroup.job2: error occured.