Quartz Scheduler Example Programs and Sample Code : Quartz Code Snippets : How-To: Initialize Job Data with Scheduler Initialization
How-To: Initialize Job Data with Scheduler Initialization
You can initialize the Scheduler with predefined jobs and triggers using the XMLSchedulingDataProcessorPlugin. An example is provided in the Quartz distribution in the directory examples/example10. However, the following is a short description of how the plugin works.
First of all, you must explicitly specify that you want to use the XMLSchedulingDataProcessorPlugin. This is an excerpt from an example quartz.properties:
#===================================================
# Configure the Job Initialization Plugin
#===================================================
org.quartz.plugin.jobInitializer.class =
org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = jobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
The following describes what each property does:
*fileNames is a comma separated list of filenames (with paths). These files contain the XML definition of jobs and associated triggers. An example jobs.xml definition file is shown later in this topic.
*failOnFileNotFound specifies whether the plugin should thrown an exception if the XML definition files are not found, thus preventing it (the plugin) from initializing?
*scanInterval specifies whether the XML definition files are to be reloaded if a change is detected in one of the files. This property specifies the interval (in seconds) at which the files are checked for changes. Set to 0 to disable reloading.
*wrapInUserTransaction must be set to true when using the XMLSchedulingDataProcessorPlugin with JobStoreCMT. Otherwise, you might experience erratic behavior.
The jobs.xml file (or any other name you assign to this file in the fileNames property) declaratively defines jobs and triggers. It can also contain directives to delete existing data. Here's a self-explanatory example:
<?xml version='1.0' encoding='utf-8'?>
<job-scheduling-data
  xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData
  http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">
<schedule>
<job>
<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>
<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>
</schedule>
</job-scheduling-data>
You can find another example of the jobs.xml file in the examples/example10 directory of your Quartz distribution.
Checkout the XML schema at http://www.quartz-scheduler.org/xml/job_scheduling_data_2_0.xsd for full details of what is possible.
Copyright © 2010-2016 Software AG, Darmstadt, Germany.

Product Logo |   Feedback