Struts 1 + Spring 2.5.6 + Quartz 1.6 scheduler example

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

Tools Used :

  1. Struts 1.3.10
  2. Spring 2.5.6
  3. Quartz 1.6.3

1. Scheduler Task

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

File : 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.

File : 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.

File : 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.

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 scheduled job automatically.

In this example, the printMessage() method will be executed 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

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
5 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
PradeepStruts 2 + Spring 2.5.6 + Quartz scheduler integration exampleGunjan ShahSpring TutorialsStruts 2 + Quartz scheduler integration example Recent comment authors
newest oldest most voted
Pradeep
Guest
Pradeep

I have seen enough System.out.println on scheduler task with quartz. How do I implement real example. Like executing service registered with spring bean from SchedulerJob?

trackback
Struts 2 + Spring 2.5.6 + Quartz scheduler integration example

[…] Struts + Spring + Quartz integration example This article is under – Struts 2.x Tutorials , Tags: Quartz , Spring , Struts2 mkyong Founder and Chief Editor of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. You might also like following tutorials : GAE TutorialsJava I/O TutorialsApache Wicket TutorialsStruts 2.x Tutorials […]

Gunjan Shah
Guest
Gunjan Shah

I have implemented quartz scheduler the same way. I have used Spring 3.0.2.

But in my case the JOB is executed twice.

Here i am not able to understand How can a single thread execute the same code at same time ???

Is it a spring problem ?

Thanks,
Gunjan Shah,
M : 94 262 70 561.

trackback
Spring Tutorials

[…] + Quartz scheduler example Article about how Spring schedule a job with Quartz framework.Spring + Struts + Quartz scheduler example Integrate Spring with Struts and schedule a job with Quartz framework.Integrating Spring with Other […]

trackback
Struts 2 + Quartz scheduler integration example

[…] Struts + Spring + Quartz integration example Oracle Magazine (Free) Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company. […]