Since Quartz 1.6, you can initialize the scheduler with predefined jobs and triggers using the JobInitializationPlugin. An example is provided in the Quartz distribution in the directory examples/example10. However, following is a short description of how the plugin works.

First of all, we need to explicitily specify in the scheduler properties that we want to use the JobInitializationPlugin. This is an excerpt from an example quartz.properties:

#===================================================
# Configure the Job Initialization Plugin
#===================================================

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileNames = jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false

Let's see what each property does:

  • fileNames: a comma separated list of filenames (with paths). These files contain the xml definition of jobs and associated triggers. We'll see an example jobs.xml definition shortly.
  • overWriteExistingJobs: tells the plugin whether or not to overwrite existing jobs with the same name at startup time.
  • failOnFileNotFound: if the xml definition files are not found, should the plugin throw an exception, thus preventing itself (the plugin) from initializing?
  • scanInterval: the xml definition files can be reloaded if a file change is detected. This is the interval (in seconds) the files are looked at. Set to 0 to disable scanning.
  • wrapInUserTransaction: if using the JobInitializationPlugin with JobStoreCMT, be sure to set the value of this property to true, otherwise you might experience unexpected behaviour.

The jobs.xml file (or any other name you use for it in the fileNames init plugin property) declaratively defines jobs, triggers, calendars and the association between jobs and triggers. Here's an autoexplicative example:

<?xml version='1.0' encoding='utf-8'?>
<quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opensymphony.com/quartz/JobSchedulingData
  http://www.opensymphony.com/quartz/xml/job_scheduling_data_1_5.xsd"
  version="1.5">
    <job>
        <job-detail>
            <name>my-very-clever-job</name>
            <group>MYJOB_GROUP</group>
            <description>The job description</description>
            <job-class>com.acme.scheduler.job.CleverJob</job-class>
            <job-data-map allows-transient-data="false">
                <entry>
                    <key>burger-type</key>
                    <value>hotdog</value>
                </entry>
                <entry>
                    <key>dressing-list</key>
                    <value>ketchup,mayo</value>
                </entry>
            </job-data-map>
        </job-detail>
        <trigger>
            <cron>
                <name>my-trigger</name>
                <group>MYTRIGGER_GROUP</group>
                <job-name>my-very-clever-job</job-name>
                <job-group>MYJOB_GROUP</job-group>
                <!-- trigger every night at 4:30 am -->
                <!-- do not forget to light the kitchen's light -->
                <cron-expression>0 30 4 * * ?</cron-expression>
            </cron>
        </trigger>
    </job>
</quartz>

A further jobs.xml example is in the examples/example10 directory of the Quartz distribution.