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.


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 {
	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 codesjava
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.

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.


Sign Up/ Sign In
Ask a Question

Copyright © 2018 CodesJava Protection Status SiteMap Reference: Java Wiki