Quartz: Enterprise Open Source Job Scheduler – Part 2

DZone 's Guide to

Quartz: Enterprise Open Source Job Scheduler – Part 2

Here's more about Quartz, and learning how to configure a job and trigger declaratively.

· Integration Zone ·
Free Resource

In the last post, we saw how to create a scheduler, a job, and the configuration of the job programmatically. Now we will see how we can configure the job and the trigger declaratively. To do this, we need to enable quartz plugins in the quartz.properties file. The configuration of quartz.properties is given below.

org.quartz.plugin.jobInitializer.class: org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames: mailsenderjobs.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound: true
org.quartz.plugin.jobInitializer.scanInterval: 120
org.quartz.plugin.jobInitializer.wrapInUserTransaction: false

Let us configure the job through XML file. This will allow us to change the job configuration without changing the source code. The job and the trigger configuration is given as part of  mailsenderjobs.xml.

<?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">
<description>This job will trigger mail service for every minute</description>
<!-- trigger every minute-->
<cron-expression>0 0/1 * 1/1 * ? *</cron-expression>

Write a scheduler called MailScheduler.

package com.smarttechies.scheduler;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
public class MailScheduler {
* @param args
public static void main(String[] args)  {
try {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
} catch (SchedulerException se) {

When the scheduler gets started it reads the configuration from the quartz.properties file. If you look at the quartz.properties, we have specified the job configuration file(mailsenderjobs.xml) for  the property “org.quartz.plugin.jobInitializer.fileNames”. This approach provides the flexibility to change the job configurations without stopping and starting of the scheduler. The quartz scheduler will detect the file changes and updates the  configuration automatically. If you look at the scheduler log, we will see that the scheduler is reading the information from the mailsenderjobs.xml file.

2016-03-19 11:28:19 INFO  XMLSchedulingDataProcessorPlugin:202 - Registering Quartz Job Initialization Plug-in.
2016-03-19 11:28:19 INFO  RAMJobStore:154 - RAMJobStore initialized.
2016-03-19 11:28:19 INFO  QuartzScheduler:268 - Scheduler meta-data: Quartz Scheduler (v2.1.7) 'MailScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 3 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
2016-03-19 11:28:19 INFO  StdSchedulerFactory:1324 - Quartz scheduler 'MailScheduler' initialized from default file in current working dir: 'quartz.properties'
2016-03-19 11:28:19 INFO  StdSchedulerFactory:1328 - Quartz scheduler version: 2.1.7
2016-03-19 11:28:19 INFO  <strong>XMLSchedulingDataProcessor:498 - Parsing XML file: mailsenderjobs.xml with systemId: mailsenderjobs.xml</strong>
2016-03-19 11:28:20 INFO  XMLSchedulingDataProcessor:1021 - Adding 1 jobs, 1 triggers.
2016-03-19 11:28:20 INFO  XMLSchedulingDataProcessor:1046 - Adding job: MYJOB_GROUP.mailSenderJob
2016-03-19 11:28:20 INFO  QuartzScheduler:534 - Scheduler MailScheduler_$_NON_CLUSTERED started.
2016-03-19 11:29:00 INFO  <strong>MailSenderJob:14 - The mail sender job triggerd</strong>

If you have multiple job configuration XML files we can configure them as comma separated list(jobs1.xml,jobs2.xml) to the property "org.quartz.plugin.jobInitializer.fileNames" . The source code of this post is available here.

integration, java, java job scheduling, job scheduler, quartz

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}