In this tutorial, you will integrate Struts + Spring + Quartz framework together to perform a scheduler task. Spring comes with ready solution to integrate the Struts and Quartz easily. The relationship as following :

Struts <--(Plug-In)--> Spring <--(Spring-Helper)--> Quartz <---> Scheduler task

1. Scheduler Task

Create a scheduler task, and the printMessage() is the method you want to schedule.

SchedulerTask.java

package com.mkyong.common.quartz;
 
public class SchedulerTask 
{
   public void printMessage() {
	System.out.println("Struts + Spring + Quartz integration example ~");
   }
}

2. Scheduler Job

To integrate Spring with Quartz, create a SchedulerJob which extends the Spring’s QuartzJobBean, instead of the Quartz Job class.

SchedulerJob.java

package com.mkyong.common.quartz;
 
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
 
public class SchedulerJob extends QuartzJobBean
{
	private SchedulerTask schedulerTask;
 
	public void setSchedulerTask(SchedulerTask schedulerTask) {
		this.schedulerTask = schedulerTask;
	}
 
	protected void executeInternal(JobExecutionContext context)
	throws JobExecutionException {
 
		schedulerTask.printMessage();
 
	}
}

3. Spring’s Quartz Helper

Spring comes with many Quartz helper classes to simplify the overall Quartz scheduler processes – Scheduler, Trigget, Job and JobDetails.

spring-scheduler.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
  <!-- Scheduler task -->
  <bean name="schedulerTask" 
           class="com.mkyong.common.quartz.SchedulerTask" />
 
   <!-- Scheduler job -->
   <bean name="schedulerJob"
	   class="org.springframework.scheduling.quartz.JobDetailBean">
 
     <property name="jobClass" 
           value="com.mkyong.common.quartz.SchedulerJob" />
 
     <property name="jobDataAsMap">
	<map>
	   <entry key="schedulerTask" value-ref="schedulerTask" />
	 </map>
      </property>
 
   </bean>
 
   <!-- Cron Trigger -->
   <bean id="cronTrigger"
	class="org.springframework.scheduling.quartz.CronTriggerBean">
 
	<property name="jobDetail" ref="schedulerJob" />
	<property name="cronExpression" value="0/5 * * * * ?" />
 
   </bean>
 
   <!-- Scheduler -->
   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	<property name="jobDetails">
	   <list>
	      <ref bean="schedulerJob" />
	   </list>
	</property>
 
	<property name="triggers">
	    <list>
		<ref bean="cronTrigger" />
	    </list>
	</property>
   </bean>
 
</beans>

4. Struts

To integrate Spring with Struts, you need to include the Spring’s ContextLoaderPlugIn into the Struts configuration file.

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>
 
   <!-- Spring Struts plugin -->
   <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
	<set-property property="contextConfigLocation"
	value="/WEB-INF/spring-scheduler.xml" />
    </plug-in>
 
</struts-config>

5. How it works

During Struts initialization, it will start the Spring Ioc container via the Spring’s ContextLoaderPlugIn Struts plug-in; While Spring initialization, it will start the Quartz scheduler automatically. In this example, the printMessage() method will execute in every 5 seconds.

Download Source Code

References

For more details explanation, you can reference to the following tutorials

  1. Struts + Spring integration example
  2. Spring + Quartz scheduler integration example
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at Struts 1.x Tutorials