Struts 2 + Quartz 2 Scheduler Integration Example

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.

Software Used in Example

To develop the example, we are using software and tools as follows.
1. Java 8
2. Quartz 2
3. Tomcat 8
4. Maven
5. Eclipse

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.
pom.xml
  <dependencies>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-core</artifactId>
		<version>2.3.16</version>
	</dependency>
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts2-convention-plugin</artifactId>
		<version>2.3.8</version>
	</dependency>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<version>3.1.0</version>
	</dependency>
	<dependency>
		<groupId>org.quartz-scheduler</groupId>
		<artifactId>quartz</artifactId>
		<version>2.2.1</version>
	</dependency>
  </dependencies> 

Create a Job

We are creating our job implementing quartz Job and overriding execute() method.
DatePrintJob.java
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 {
	@Override
	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.
QuartzListener.java
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;
@WebListener
public class QuartzListener implements ServletContextListener {
	@Override
	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);
			sch.start();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
	@Override
	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.

web.xml

Find the web.xml for struts 2.
web.xml
<web-app>
  <display-name>Struts 2 Annotation Example</display-name>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.concretepage.action</param-value>
        </init-param>
    </filter>
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app> 

Output

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

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us