ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

What Is Quartz
Pages: 1, 2, 3, 4, 5

Calling Your Jobs with the Scheduler

Creating the Job is the first step, but to get your Job called by the Scheduler, you need to indicate to the Scheduler how often, and at what time, your Job should be called. This is done by associating a Trigger to the Job. Since we are only interested in calling the Job every 60 seconds or so, and repeating this Scheduler forever, we are going to use a SimpleTrigger.

Jobs and Triggers are scheduled through the Quartz Scheduler interface. In order to get an instance of the Scheduler, we need to create one from the factory. The easiest way to do this is to call the static method getDefaultScheduler() on the StdSchedulerFactory class.

When using the Quartz framework, you're required to start the Scheduler by calling the start() method. The code in Example 3 follows a common pattern of most Quartz applications: create one or more Jobs, create and set up the Triggers, schedule Jobs and Triggers with the Scheduler, and start the Scheduler. (Note: the Scheduler could have been started first, it doesn't matter.)

Example 3. Quartz Jobs have to be scheduled through the Quartz Scheduler.

public class MyQuartzServer {

    public static void main(String[] args) {
        MyQuartzServer server = new MyQuartzServer();

        try {
        } catch (SchedulerException ex) {

    protected void startScheduler() throws SchedulerException {

        // Use the factory to create a Scheduler instance
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        // JobDetail holds the definition for Jobs
        JobDetail jobDetail = 
new JobDetail("ScanFTPJob", Scheduler.DEFAULT_GROUP,

// Store job parameters to be used within execute()

        // Other neccessary Job parameters here

        // Create a Trigger that fires every 60 seconds
        Trigger trigger = TriggerUtils.makeSecondlyTrigger(60);
        // Setup the Job and Trigger with the Scheduler
        scheduler.scheduleJob(jobDetail, trigger );
        // Start the Scheduler running

Programmatic vs. Declarative Scheduling

In Example 3, we scheduled our ScanFTPSiteJob using a programmatic approach. That is, we used Java code to setup the Job and Trigger with the Scheduler. The Quartz framework also supports setting up the Job schedule declaratively in XML files. A declarative approach allows us to more quickly modify which Jobs are being executed and when.

The Quartz framework includes a "plugin" that reads an XML file containing Job and Trigger information upon startup of a Quartz application. All Jobs within the XML are added to the Scheduler, along with their associated Triggers. You still have to write the Job classes, of course, but configuring the Scheduler with those Job becomes very dynamic. Example 4 shows an example XML file that performs the same logic as the code in Example 3, but just in a declarative fashion.

Example 4. Quartz Jobs can also be scheduled using an XML File.

<?xml version='1.0' encoding='utf-8'?>
                A job that scans an ftp site for files

            <job-data-map allows-transient-data="true">
                <!--  Other neccessary Job parameters here -->


                <start-time>2005-09-11 6:10:00 PM</start-time>
                <!-- repeat indefinitely every 60 seconds -->


You should be able to match the XML elements from Example 4 with the Java code from Example 3--they are conceptually the same. The benefit of using a declarative approach like the one shown in Example 4 is that maintenance becomes extremely simple, as changes only require a change to the XML file and a restart of the Quartz application. No source code changes, no recompilation, and no redeployment of the new build.

Pages: 1, 2, 3, 4, 5

Next Pagearrow