Quartz Scheduler Configuration For Web Applications

                     quartz scheduler In this article, we will discuss how to configure Quartz with the web application. To demonstrate, I have created a sample web application and the code is available at https://github.com/2013techsmarts/Quartz_Proj/tree/master/QuartzDemo

                     After the creation of the web project, copy all the quartz related jar files (quartz-all-2.1.7.jar, slf4j-api-1.6.1.jar, slf4j-log4j12-1.6.1.jar, log4j-1.2.16.jar, javax.transaction.jar, c3p0-0.9.1.1.jar) into the lib directory of the web application. This will make availability of all the jar files in the classpath. Put the quartz.properties and jobs XML file under classes folder.

                    Now, create the scheduler job as given below.


package com.smarttechies.job;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MailSenderJob implements Job{

 Logger log = Logger.getLogger(MailSenderJob.class);

 @Override
 public void execute(JobExecutionContext pArg0) throws JobExecutionException {
 log.info("The mail sender job triggerd");
 //From here call the mail sender service
 }

}

The job configuration of the above job is defined in the below XML file.






 
 mailSenderJob
 MYJOB_GROUP
 This job will trigger mail service for every minute
 com.smarttechies.job.MailSenderJob
 


 
 mailSenderTrigger
 MYTRIGGER_GROUP
 mailSenderJob
 MYJOB_GROUP
 <!-- trigger every -->
 0 0/1 * 1/1 * ? *
 




Now, we need to configure the Quartz listener in the web.xml. This listener creates Quartz scheduler and initializes the job when the application is deployed. The web.xml configuration is given below.


	
 	
 org.quartz.ee.servlet.QuartzInitializerListener
 


Now, build and deploy the web application. The log of the web container will depict that the job is initialized and triggered.


15:15:19,671 INFO [STDOUT] 15:15:19,671 INFO [QuartzInitializerListener] Quartz Initializer Servlet loaded, initializing Scheduler...
15:15:19,827 INFO [STDOUT] 15:15:19,827 INFO [StdSchedulerFactory] Using default implementation for ThreadExecutor
15:15:19,905 INFO [STDOUT] 15:15:19,905 INFO [SchedulerSignalerImpl] Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
15:15:19,905 INFO [STDOUT] 15:15:19,905 INFO [QuartzScheduler] Quartz Scheduler v.2.1.7 created.
15:15:19,921 INFO [STDOUT] 15:15:19,921 INFO [XMLSchedulingDataProcessorPlugin] Registering Quartz Job Initialization Plug-in.
15:15:19,921 INFO [STDOUT] 15:15:19,921 INFO [RAMJobStore] RAMJobStore initialized.
15:15:19,921 INFO [STDOUT] 15:15:19,921 INFO [QuartzScheduler] 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.
15:15:19,921 INFO [STDOUT] 15:15:19,921 INFO [StdSchedulerFactory] Quartz scheduler 'MailScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
15:15:19,921 INFO [STDOUT] 15:15:19,921 INFO [StdSchedulerFactory] Quartz scheduler version: 2.1.7
15:15:20,061 INFO [STDOUT] 15:15:20,061 INFO [XMLSchedulingDataProcessor] Parsing XML file: mailsenderjobs.xml with systemId: mailsenderjobs.xml
15:15:20,233 INFO [STDOUT] 15:15:20,233 INFO [XMLSchedulingDataProcessor] Adding 1 jobs, 1 triggers.
15:15:20,233 INFO [STDOUT] 15:15:20,233 INFO [XMLSchedulingDataProcessor] Adding job: MYJOB_GROUP.mailSenderJob
15:15:20,249 INFO [STDOUT] 15:15:20,249 INFO [QuartzScheduler] Scheduler MailScheduler_$_NON_CLUSTERED started.
15:15:20,249 INFO [STDOUT] 15:15:20,249 INFO [QuartzInitializerListener] Scheduler has been started...
15:15:20,249 INFO [STDOUT] 15:15:20,249 INFO [QuartzInitializerListener] Storing the Quartz Scheduler Factory in the servlet context at key: org.quartz.impl.StdSchedulerFactory.KEY
15:15:20,280 INFO [STDOUT] 15:15:20,280 WARN [FileScanJob] File 'mailsenderjobs.xml' does not exist.

Advertisement

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.

Posted in Quartz Scheduler
7 comments on “Quartz Scheduler Configuration For Web Applications
  1. Oliver says:

    Great article!!!
    I’m using this tutorial and trying to set the cron timer programatically. Do you have any suggestion on how should I do it?

    Thanks in advance!

  2. Richelle says:

    I got this website from my friend .I am browsing this web page and reading very informative content.

  3. przeczytaj says:

    Nice Post…
    Thanks

  4. Anonymous says:

    Hi! I’m undoubtedly enjoying your blog and look forward to new posts.

  5. Ram says:

    Hey There. I found your blog using msn. This is an extremely well written article.
    I’ll be sure to bookmark it and return to read more of your useful information. Thanks for the post. I will definitely return.

  6. zapoznaj artykułami says:

    Excellent site you have here.

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 )

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.

Dzone.com
DZone

DZone MVB

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