CodesJava

Easy learning with example program codes

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 codesjava
 */
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 codesjava
 */
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.

 

Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status