Java Reference
In-Depth Information
Scheduler
Job
Trigger
Figure 6-1. The Quartz scheduler
As you can see, Quartz has three main components: a scheduler, a job, and a trigger. A scheduler,
which is obtained from a SchedulerFactory , serves as a registry of JobDetails (a reference to a Quartz
job) and triggers and is responsible for executing a job when its associated trigger fires. A job is a unit of
work that can be executed. A trigger defines when a job is to be run. When a trigger fires, telling Quartz to
execute a job, a JobDetails object is created to define the individual execution of the job.
Does this sound familiar? It should. The model of defining a job and a JobDetails object is very
similar to the way Spring Batch defines a job and a JobInstance . In order to integrate Quartz with your
Spring Batch process, you need to do the following:
Add the required dependencies to your pom.xml file.
Write your own Quartz job to launch your job using Spring's QuartzJobBean .
Configure a JobDetailBean provided by Spring to create a Quartz JobDetail .
Configure a trigger to define when your job should run.
To show how Quartz can be used to periodically execute a job, let's create a new job,
deleteFilesJob , that empties a directory each time it runs. This is a common practice with batch jobs or
any instance where data is stored and needs to be periodically removed (database purges, and so on). In
this case, you're deleting all files in a temporary directory.
You begin by adding the required dependencies to your POM file. In this case, there are three new
dependencies. The first is the Quartz framework itself. The second dependency you add is for the
spring-context-support artifact. This package from Spring provides the classes required to integrate
Quartz easily with Spring. Finally, to help with some of the configuration hoops you're jumping through
for this example, you include the Apache Commons Collections library. The configuration for the
dependency is found in Listing 6-8.
Listing 6-8. Adding the Quartz-Related Dependencies to the POM
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.3 /version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
 
Search WWH ::




Custom Search