Home  >  Quartz

Quartz 2 Scheduler Job Exception Handling Example with JobExecutionException

By Arvind Rai, May 25, 2015
Here we will walk through the quartz 2 scheduler job exception handling example with JobExecutionException. JobExecutionException has different methods like setRefireImmediately(), refireImmediately() and setUnscheduleAllTriggers(). By calling these methods, we give the signal to quartz whether to refire the job immediately or un schedule 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.

JobExecutionException

org.quartz.JobExecutionException is thrown by execute() 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);
 
It has different methods to indicate quartz what to do after exception.
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.

Job Exception Handling with 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;
		}
	}
} 

ExceptionHandlingDemoOne.java
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);
	}
} 
In this case even after getting exception, quartz will fire all number of defined triggers. Find the output.
mygroup.job1: error occured.
mygroup.job1: error occured.
mygroup.job1: error occured. 

Job Exception Handling with 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;
		}
	}
} 

ExceptionHandlingDemoTwo.java
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);
	}
} 
In this case after getting exception, quartz will un schedule all triggers. Find the output.
mygroup.job2: error occured.
 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us