Pages Navigation Menu

Coding is much easier than you think

Integrating Quartz Scheduler with Java Web Application

In our previous article we learn about Quartz 2 hello world example along with types of Triggers in Quartz 2, In this article we shall learn to Integrating Quartz 2 Scheduler in Java Web Application. Quartz, is an open source job scheduling framework, that let you schedule a task to run on a predefine date and time.

Library required


Scheduler Job

Create a Quartz’s job

package com.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class QuartzJob implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("Java web application + Quartz 2.2.1");


Servlet Listener

Now Create servlet listener class by implementing ServletContextListener interface and override contextInitialized and contextDestroyed methods with your logic’s.

  • contextInitialized() method will be executed automatically during Servlet container initialization, which in turn calls the Quartz scheduler job, which gets executed every 10 seconds.
  • contextDestroyed() method will be executed when the application shuts down, So in this function I have invoked the shutdown function of quartz scheduler


package com.quartz;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzListener implements ServletContextListener {
	Scheduler scheduler = null;

	public void contextInitialized(ServletContextEvent servletContext) {
		System.out.println("Context Initialized");
		try {
			// Setup the Job class and the Job group
			JobDetail job = newJob(QuartzJob.class).withIdentity(
					"CronQuartzJob", "Group").build();

			// Create a Trigger that fires every 5 minutes.
			Trigger trigger = newTrigger()
			.withIdentity("TriggerName", "Group")
			.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))

			// Setup the Job and Trigger with Scheduler & schedule jobs
			scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.scheduleJob(job, trigger);
		catch (SchedulerException e) {

	public void contextDestroyed(ServletContextEvent servletContext) {
		System.out.println("Context Destroyed");
		catch (SchedulerException e) 

Recommended Article



Configure the listener class into the web.xml file as shown below




Now on starting the Tomcat Server, the project gets started, and so the registered listener class will be fired, and following output is obtained at the console
Integrating Quartz Scheduler with Java Web Applications

About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: