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">
<schedule>
<job>
<name>mailSenderJob</name>
<group>MYJOB_GROUP</group>
<description>This job will trigger mail service for every minute</description>
<job-class>com.smarttechies.job.MailSenderJob</job-class>
</job>
<trigger>
<cron>
<name>mailSenderTrigger</name>
<group>MYTRIGGER_GROUP</group>
<job-name>mailSenderJob</job-name>
<job-group>MYJOB_GROUP</job-group>
<!-- trigger every minute-->
<cron-expression>0 0/1 * 1/1 * ? *</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>

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();
scheduler.start();
} catch (SchedulerException se) {
   se.printStackTrace();
}
}
}

      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.
NOT STARTED.
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.

Advertisements

I am Siva Prasad Rao Janapati. Working as a software developer. Has hands on experience on ATG Commerce(DAS/DPS/DCS), Mozu commerce, Broadleaf Commerce, Java, JEE, Spring, Play, JPA, Hibernate, Velocity, JMS, Jboss, Weblogic,Tomcat, Jetty, Apache, Apache Solr, Spring Batch, JQuery, NodeJS, SOAP, REST, MySQL, Oracle, Mongo DB, Memcached, HazelCast, Git, SVN, CVS, Ant, Maven, Gradle, Amazon Web services, Rackspace, Quartz, JMeter, Junit, Open NLP, Facebook Graph,Twitter4J, YouTube Gdata, Bazzarvoice,Yotpo, 4-Tell, Alatest, Shopzilla, Linkshare. I have hands on experience on open sources and commercial technologies.

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 )

Google+ photo

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

Connecting to %s

DZone

DZone MVB

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