Struts 2 + Quartz 2 Scheduler Integration

By Arvind Rai, May 29, 2015
This page will explain Struts 2 and Quartz 2 scheduler integration example. With the help of ServletContextListener we can use quartz with Struts 2. Struts 2 does not provide any specific plugin to integrate quartz scheduler. We implement a listener using ServletContextListener and write quartz code in contextInitialized() method. On server startup, scheduler is started. In our example we have created a simple job and to schedule it, we are using CronTrigger that will accept the given cron-expression.

Demo Project Structure in Eclipse

Find the print screen of project structure in eclipse.
Struts 2 + Quartz 2 Scheduler Integration Example

Maven Dependency

Find the maven dependencies for struts 2 and quartz jar.

Create a Job

We are creating our job implementing quartz Job and overriding execute() method.
package com.concretepage.job;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
public class DatePrintJob implements Job {
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		JobDetail jobDetail = context.getJobDetail();
		JobKey key = jobDetail.getKey();
		System.out.println("Job key:"+ key +", Date:"+ new Date());

ServletContextListener for Quartz

We are creating a listener compatible to servlet 3 using @WebListener. Our listener is implementing ServletContextListener. This listener will be started on server startup. Inside the contextInitialized() method, we write code for quartz scheduler as usual using quartz API.
package com.concretepage.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import com.concretepage.job.DatePrintJob;
public class QuartzListener implements ServletContextListener {
	public void contextInitialized(ServletContextEvent sce) {
	    try {
			SchedulerFactory schfa = new StdSchedulerFactory();
			Scheduler sch = schfa.getScheduler();
			JobDetail jobdetail = JobBuilder.newJob(DatePrintJob.class).withIdentity("myjob", "mygroup").build();
			//Executes after every minute 0 0/1 * 1/1 * ? *
			CronTrigger crontrigger = TriggerBuilder.newTrigger().withIdentity("crontrigger", "mygroup")
				    .withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * 1/1 * ? *")).build();
			sch.scheduleJob(jobdetail, crontrigger);
		} catch (SchedulerException e) {
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("Inside Context Destroyed method.");
In our example CronTrigger is being used which needs cron-expression to schedule the job.


Find the web.xml for struts 2.
  <display-name>Struts 2 Annotation Example</display-name>


Build the project and create WAR using Maven and deploy it. On server startup, our QuartzListener will start and schedule the DatePrintJob. Job will run every after 1 minute.
Job key:mygroup.myjob, Date:Fri May 29 12:44:00 IST 2015
Job key:mygroup.myjob, Date:Fri May 29 12:45:00 IST 2015
Job key:mygroup.myjob, Date:Fri May 29 12:46:00 IST 2015
Job key:mygroup.myjob, Date:Fri May 29 12:47:00 IST 2015
Job key:mygroup.myjob, Date:Fri May 29 12:48:00 IST 2015 

Download Source Code


©2024 | Privacy Policy | Contact Us