Example 6 - Dealing with Job Exceptions
This example is designed to demonstrate how to deal with job execution exceptions. Jobs in Quartz are permitted to throw a JobExecutionExceptions. When this exception is thrown, you can direct Quartz to take a specified action.
The program will perform the following actions:
Start up the Quartz Scheduler.
Schedule two jobs. Each job will execute every three seconds for an indefinite period of time.
The jobs will throw exceptions and Quartz will take appropriate action.
The program will wait 60 seconds so that the two jobs have plenty of time to run.
Shut down the scheduler.
Running the Example
This example can be executed from the examples/example6 directory. There are two ways to run this example
example6.sh - A UNIX/Linux shell script
example6.bat - A Windows Batch file
The Code
The code for this example resides in the package org.quartz.examples.example6.
The code in this example is made up of the following classes:
Class Name | Description |
JobExceptionExample | The main program. |
BadJob1 | A simple job that will throw an exception and instruct Quartz to refire its trigger immediately. |
BadJob2 | A simple job that will throw an exception and instruct Quartz to never schedule the job again. |
BadJob1
BadJob1 is a simple job that creates an artificial exception (divide by zero). When this exception occurs, a JobExecutionException is thrown and set to refire the job immediately.
try {
int zero = 0;
int calculation = 4815 / zero;
}
catch (Exception e) {
_log.info("--- Error in job!");
JobExecutionException e2 =
new JobExecutionException(e);
// this job will refire immediately
e2.refireImmediately();
throw e2;
}
This will force Quartz to run this job over and over again.
BadJob2
BadJob2 is a simple job that creates an artificial exception (divide by zero). When this exception occurs, a JobExecutionException is thrown and set to ensure that Quartz never runs the job again.
try {
int zero = 0;
int calculation = 4815 / zero;
}
catch (Exception e) {
_log.info("--- Error in job!");
JobExecutionException e2 =
new JobExecutionException(e);
// Quartz will automatically unschedule
// all triggers associated with this job
// so that it does not run again
e2.setUnscheduleAllTriggers(true);
throw e2;
}
This will force Quartz to shutdown this job so it does not run again.
JobExceptionExample
The program starts by getting an instance of the Scheduler. This is done by creating a StdSchedulerFactory and then using it to create a scheduler. This will create a simple, RAM-based scheduler.
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
Job #1 is scheduled to run every 3 seconds indefinitely. This job will fire BadJob1.
JobDetail job = newJob(BadJob1.class)
.withIdentity("badJob1", "group1")
.build();
SimpleTrigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startAt(startTime)
.withSchedule(simpleSchedule()
.withIntervalInSeconds(3)
.repeatForever())
.build();
Date ft = sched.scheduleJob(job, trigger);
Job #2 is scheduled to run every 3 seconds indefinitely. This job will fire BadJob2.
job = newJob(BadJob2.class)
.withIdentity("badJob2", "group1")
.build();
trigger = newTrigger()
.withIdentity("trigger2", "group1")
.startAt(startTime)
.withSchedule(simpleSchedule()
.withIntervalInSeconds(3)
.repeatForever())
.build();
ft = sched.scheduleJob(job, trigger);
The scheduler is then started.
sched.start();
To let the scheduler have an opportunity to run the job, our program will sleep for 1 minute (60 seconds)
Thread.sleep(60L * 1000L);
The scheduler will run both jobs (BadJob1 and BadJob2). Each job will throw an exception. Job 1 will attempt to refire immediately. Job 2 will never run again.
Finally, the program gracefully shuts down the scheduler:
sched.shutdown(true);
Note: | Passing "true" to the shutdown method tells Quartz Scheduler to wait until all jobs have completed running before returning from the method call. |