Struts 2 and Quartz 2 scheduler integration

To integrate Struts 2 and Quartz 2 scheduler we have to use a standard Servlet Listener which links both frameworks together. To create a standard Servlet Listener class implements ServletContextListener interface. ServletContextListener interface provides contextInitialized() method which is executed automatically at the time of Servlet container initialization. Servlet Listener invokes the Quartz scheduler framework in this method. 
Note: Do the listener class entry into web.xml file.

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
* This class is used for executing quartz job
* using CronTrigger(Quartz 2.1.5).
* @author javawithease

public class QuartzListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("contextDestroyed is called.");
public void contextInitialized(ServletContextEvent arg0) {
//Set job details.
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity("helloJob", "group1").build();
//Set the scheduler timings.
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("cronTrigger", "group1")
.cronSchedule("0/10 * * * * ?")).build();
//Execute the job.
Scheduler scheduler =
new StdSchedulerFactory().getScheduler();
scheduler.scheduleJob(job, trigger);
}catch(Exception e){
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
* This class defines a quartz job.
* @author javawithease

public class HelloJob implements Job{
public void execute(JobExecutionContext context)
throws JobExecutionException {
System.out.println("Hello World.");
<?xml version="1.0" encoding="UTF-8"?>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"" >


Hello World.
Hello World.
Hello World.
Hello World.
Hello World.

No comments: