• September 4, 2014
  • Quartz Scheduler
  • Comments Off on Struts 2 and Quartz 2 scheduler integration

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.

Example:

QuartzListener.java

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 {
 
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("contextDestroyed is called.");
	}
 
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		try{
    		//Set job details.
    		JobDetail job = JobBuilder.newJob(HelloJob.class)
    			.withIdentity("helloJob", "group1").build();
 
        	//Set the scheduler timings.
    		Trigger trigger = TriggerBuilder.newTrigger()
    			.withIdentity("cronTrigger", "group1")
    			.withSchedule(CronScheduleBuilder
    			  .cronSchedule("0/10 * * * * ?")).build();
 
        	//Execute the job.
    		Scheduler scheduler = 
    			new StdSchedulerFactory().getScheduler();
        	scheduler.start();
        	scheduler.scheduleJob(job, trigger);
    	}catch(Exception e){
    		e.printStackTrace();
    	}    		
	}
}

HelloJob.java

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.");		 
	}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
	<filter>
	  <filter-name>struts2</filter-name>
	  <filter-class>
            org.apache.struts2.dispatcher.ng.
                      filter.StrutsPrepareAndExecuteFilter
          </filter-class>
	</filter>
 
	<filter-mapping>
	  <filter-name>struts2</filter-name>
	  <url-pattern>/*</url-pattern>
	</filter-mapping>
 
	<listener>
	  <listener-class>
            com.javawithease.business.QuartzListener
          </listener-class>
	</listener>
 
</web-app>

Output:

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

Download this example.
 
Next Topic: JDBC tutorial with example.
Previous Topic: Struts 2 and tiles integration with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.

 


Comments are closed.