Pages Navigation Menu

Coding is much easier than you think

Integrating Quartz Scheduler in Struts 2 Web Application

Posted by on Aug 6, 2013 in Quartz Scheduler, Struts-2 | 1 comment

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 Struts 2 web Application. To know more about Quartz please visit its Official website.

Library required

Commonly Required Struts 2 Jars

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("Struts 2 + 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.

  • In contextInitialized() method method I have written a code to start the Quartz Scheduler, and since this method will be executed automatically during Servlet container initialization, hence the code for Quartz scheduler job gets invoked, and so it runs for 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

<display-name>Quartz 2 + Struts 2</display-name>



Now on starting the Tomcat Server, the project gets started, and so the listener class registered in web.xml will be fired, and following logs is obtained at the console
Struts 2 + Quartz logs

Read More