Struts + Quartz scheduler integration example
In this tutorials, you will create a simple web application project with Apache Struts 1.x, during project start up, it will start the Quartz engine and fire the declared scheduler task.
P.S Please visit this Quartz scheduler example to learn the basic usage of the Quartz framework.
The overall idea is quite simple, you just need to create a Quartz Struts plug-in for the integration work.
Struts <--> Quartz Struts Plug-in <--> Quartz
1. Quartz Scheduler
Create your scheduler task and the method you want to schedule. In this example, you schedule a “printMessage()” method to print the message periodically.
SchedulerTask.java
package com.mkyong.common.quartz; public class SchedulerTask { public void printMessage() { System.out.println("Struts + Quartz integration example ~"); } }
SchedulerJob.java
package com.mkyong.common.quartz; import java.util.Map; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class SchedulerJob implements Job { public void execute(JobExecutionContext context) throws JobExecutionException { Map dataMap = context.getJobDetail().getJobDataMap(); SchedulerTask task = (SchedulerTask)dataMap.get("schedulerTask"); task.printMessage(); } }
2. Quartz Struts Plug-in
Create a Quartz Struts plug-in by extends the Struts’s “PlugIn” class, and override the init() method. During Struts initialization, this Struts plug-in init() method will be called and start the Quartz scheduler.
QuartzPlugin.java
package com.mkyong.common.plugin; import java.text.ParseException; import java.util.Map; import javax.servlet.ServletException; import org.apache.struts.action.ActionServlet; import org.apache.struts.action.PlugIn; import org.apache.struts.config.ModuleConfig; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; import com.mkyong.common.quartz.SchedulerJob; import com.mkyong.common.quartz.SchedulerTask; public class QuartzPlugin implements PlugIn { @Override public void destroy() { //null } @Override public void init(ActionServlet servlet, ModuleConfig config) throws ServletException { SchedulerTask task = new SchedulerTask(); //specify your sceduler task details JobDetail job = new JobDetail(); job.setName("jobName"); job.setJobClass(SchedulerJob.class); Map dataMap = job.getJobDataMap(); dataMap.put("schedulerTask", task); try{ //configure the scheduler time, run it every 5 seconds CronTrigger trigger = new CronTrigger(); trigger.setName("runMeJobTesting"); trigger.setCronExpression("0/5 * * * * ?"); //schedule it Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); }catch(ParseException e){ e.printStackTrace(); }catch(SchedulerException e){ e.printStackTrace(); } } }
3. Struts
Include the Quartz Struts plug-in into the Struts configuration file (struts-config.xml).
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> <struts-config> <action-mappings> <action path="/Welcome" type="org.apache.struts.actions.ForwardAction" parameter="/pages/quartz_started.jsp"/> </action-mappings> <plug-in className="com.mkyong.common.plugin.QuartzPlugin" /> </struts-config>
Done, when this Struts project is starting, it will execute the Quartz declared scheduler task. In this example, it will execute the “printMessage()” method every 5 seconds.

Once i start this project, the scheduler is not starting…Please help
Sorry….Its working fine….Jar files were missing…
Good to know it worked for you.
[...] Struts + Quartz integration example [...]
Thank you mkyong for the tutorial, working like a charm
[...] Struts + Quartz integration example [...]
[...] Struts + Quartz scheduler integration Example to integrate the Struts with Quartz framework. [...]