Quartz – Enterprise Open Source Job Scheduler – Part2

                 quartz schedulerIn the last post, we have seen, how to create a scheduler and  a job, and also the configuration of the job through 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: <strong>mailsenderjobs.xml</strong>
org.quartz.plugin.jobInitializer.failOnFileNotFound: true
org.quartz.plugin.jobInitializer.scanInterval: 120
org.quartz.plugin.jobInitializer.wrapInUserTransaction: false

Now, We need to configure the job in the XML file. This will allow us to change the job configuration without changing the source code. The mailsenderjobs.xml will contain all the job and trigger configuration. The content of the mailsenderjobs.xml is given below.

<?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>

Now, the new scheduler code is simplified. The code of MailScheduler is given below.

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”. The main advantage of this approach is, we can change the job configurations on the fly. That means, we can change the job configuration XML file, with out stopping and restarting 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.

2013-04-21 11:28:19 INFO  XMLSchedulingDataProcessorPlugin:202 - Registering Quartz Job Initialization Plug-in.
2013-04-21 11:28:19 INFO  RAMJobStore:154 - RAMJobStore initialized.
2013-04-21 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.
2013-04-21 11:28:19 INFO  StdSchedulerFactory:1324 - Quartz scheduler 'MailScheduler' initialized from default file in current working dir: 'quartz.properties'
2013-04-21 11:28:19 INFO  StdSchedulerFactory:1328 - Quartz scheduler version: 2.1.7
2013-04-21 11:28:19 INFO  <strong>XMLSchedulingDataProcessor:498 - Parsing XML file: mailsenderjobs.xml with systemId: mailsenderjobs.xml</strong>
2013-04-21 11:28:20 INFO  XMLSchedulingDataProcessor:1021 - Adding 1 jobs, 1 triggers.
2013-04-21 11:28:20 INFO  XMLSchedulingDataProcessor:1046 - Adding job: MYJOB_GROUP.mailSenderJob
2013-04-21 11:28:20 INFO  QuartzScheduler:534 - Scheduler MailScheduler_$_NON_CLUSTERED started.
2013-04-21 11:29:00 INFO  <strong>MailSenderJob:14 - The mail sender job triggerd</strong>

           If you have multiple job configuration XML files, we can give all the job configuration files as comma separated list(jobs1.xml,jobs2.xml). The source code of this post is available at https://github.com/2013techsmarts/Quartz_Proj/tree/master/Sample_Quartz_Plugin_Proj

         In the coming post, we will see how to configure Quartz as part of a web application.


Siva Janapati is an Architect with experience in building Cloud Native Microservices architectures, Reactive Systems, Large scale distributed systems, and Serverless Systems. Siva has hands-on in architecture, design, and implementation of scalable systems using Cloud, Java, Go lang, Apache Kafka, Apache Solr, Spring, Spring Boot, Lightbend reactive tech stack, APIGEE edge & on-premise and other open-source, proprietary technologies. Expertise working with and building RESTful, GraphQL APIs. He has successfully delivered multiple applications in retail, telco, and financial services domains. He manages the GitHub(https://github.com/2013techsmarts) where he put the source code of his work related to his blog posts.

Tagged with: , ,
Posted in Quartz Scheduler, Scheduler

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.



Java Code Geeks
Java Code Geeks
%d bloggers like this: