Sample uses of job scheduling with Quartz:

• System Maintenance: Schedule a job to dump the
contents of a database into an XML file every business
day (like all weekdays except holidays) at 11:30 PM.
• Cleaning Resources: Schedule a job to clean up the
unwanted debris left from the running applications. This
could be log files, temp tables in a database, clogged
connections, or log trails of transactions.
• Driving Workflow : As a new order is initially placed, schedule a Job to fire in exactly 2 hours, that will check the status of that order, and trigger a warning notification if an order confirmation message has not yet been received for the order, as well as changing the order’s
status to ‘awaiting intervention’.

Architecture of the Quartz Framework

Quartz framework contains approximately 300 Java Classes and its multiple interfaces are organized roughly into 12 packages. Quartz Framework highly depends on the Quartz Scheduler, Jobs, Triggers and the Job Stores.

The Quartz Scheduler

The Quartz Scheduler lies at the heart of the Quartz
framework. Quartz Scheduler is an engine that manages
the runtime environment of the Quartz framework. Scheduler’s queue is used to schedule the tasks by
queuing them. The framework then allots the threads
from its thread pool to execute different tasks (in the queue).

Quartz Framework provides a multi-threaded environment
through a pre-defined set of threads configured at startup to manage independent tasks (by multi-tasking them).

When the Scheduler starts up, it initializes the threads and
waits for a task to be triggered. At the specified time, the
Scheduler assigns a thread to the relevant task and waits.
When the task is completed, the thread is returned to the
Thread Pool.

Therefore during designing, it’s important to take care that
there are enough threads available for the Scheduler to run the tasks on time. If there is no thread available when a task is ready to execute, the scheduler will pause until one becomes available. Initializing too many threads can cause degradation of system performance and contention issues.

The easiest way to get a Scheduler working is to use the
default Scheduler shipped with Quartz. Call the static
getDefaultScheduler() method of the StdSchedulerFactory
class to instantiate the default Scheduler. Now the Scheduler is ready to accept Jobs and Triggers.



A Job is simply a Java class that performs a task. This
task can be anything that you can code in Java. The only
requirement is that you implement the org.quartz.Job interface and throw a JobExecutionException in the
case of a serious error.

Once you implement the Job interface and implement it’s
execute() method, Quartz will invoke the required job when it determines it’s time to run. Here are a few examples that can
be coded inside a Job:

• Use JavaMail (or another Mail framework like Commons
Net) to send emails
• Create a remote interface and invoke a method on an
• Get a Hibernate Session, and query and update data in
a relational database
• Use OSWorkflow and invoke a workflow from the Job
• Use FTP and move files around
• Call an Ant build script to kick off scheduled builds

The possibilities are endless and that’s what makes the
framework so powerful. Quartz provides you with the mechanism to establish a very granular, repeatable schedule, and then you just create Java classes that get called to execute.


Quartz’s architecture intentionally separates the
trigger from the job so that you can make jobs reusable.
Multiple jobs can be triggered from a single trigger and
multiple triggers can invoke the same job. This facilitates
easy code maintenance. Moreover, Quartz provides simple, built-in triggers to do regularly used actions. Job Groups allows you to include the same job in different Job Groups and allows for common actions in the Job Group.

Jobs are only half the equation and requires triggers to get
completed. A Trigger in Quartz is used to tell the Scheduler
when a Job should be triggered (or fired). The framework comes with a handful of Trigger types, but the two most commonly used are the SimpleTrigger and the CronTrigger.


The SimpleTrigger is required when you need a simple
firing schedule. Typically, if you require a Job to occur at
a given time and repeat (n) number of times after specific
time-intervals, then the SimpleTrigger is for you. On the
other hand, if you need a more complicated schedule for
your Job, then the CronTrigger will probably be required.


August 2007 | Java Jazz Up | 10
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40           Download PDF