Struts 2 + Spring 2.5.6 + Quartz 1.6 scheduler integration example

Struts 2 + Spring 2.5.6 + Quartz scheduler integration example

In this tutorial, we will show you how to integrate Struts 2 + Spring 2.5.6 + Quartz 1.6.5 scheduler together. The relationship look like this :


Struts 2 <-- Plugin --> Spring <--(Helper)--> Quartz <---> Scheduler task

Tools used

  1. Spring 2.5.6
  2. Quartz 1.6.3
  3. Struts 2.1.8
  4. Struts2-spring-plugin 2.1.8
  5. Maven 2
  6. Eclipse 3.6
Note
You may also interest at this – Struts 2 + Spring 3 + Quartz 1.8.6 integration example..

1. Project folder

Here’s the project folder structure.

Struts 2 Spring Quartz integration example

2. Dependency Libraries

Get all the dependency libraries, you need Spring, Struts2, Strut2-Spring-Plugin and Quartz jar file.

File : pom.xml


  ...
  <dependencies>

	<!-- Struts 2 -->
	<dependency>
          <groupId>org.apache.struts</groupId>
	  <artifactId>struts2-core</artifactId>
	  <version>2.1.8</version>
        </dependency>

	<!-- Quartz framework -->
	<dependency>
          <groupId>opensymphony</groupId>
	  <artifactId>quartz</artifactId>
	  <version>1.6.3</version>
	</dependency>
	 
	<!-- Quartz dependency library-->
	<dependency>
	  <groupId>commons-collections</groupId>
	  <artifactId>commons-collections</artifactId>
	  <version>3.2.1</version>
	</dependency>

	<!-- Spring framework --> 
	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring</artifactId>
	  <version>2.5.6</version>
	</dependency>

	<dependency>
	  <groupId>org.springframework</groupId>
	  <artifactId>spring-web</artifactId>
	  <version>2.5.6</version>
	</dependency>

	<!-- Struts 2 + Spring plugins -->
	<dependency>
          <groupId>org.apache.struts</groupId>
	  <artifactId>struts2-spring-plugin</artifactId>
	  <version>2.1.8</version>
        </dependency>

  </dependencies>
  ...

3. Scheduler Task

Put all the scheduler logic in this class.

File : SchedulerTask.java


package com.mkyong.quartz;
 
public class SchedulerTask {
	
   public void printSchedulerMessage() {
	
	   System.out.println("Struts 2 + Spring + Quartz ......");
	   
   }
}

4. Spring + Quartz

To integrate Spring and Quartz, create a class extends the org.springframework.scheduling.quartz.QuartzJobBean, reference the scheduler task (SchedulerTask.java) via a setter method, and put the scheduler logic inside the executeInternal() method.

File : SchedulerJob.java


package com.mkyong.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.printSchedulerMessage();
 
	}
}

File : applicationContext.xml – Create an applicationContext.xml file, put all the Spring + Quartz integration stuffs inside. Read XML comments for detail.


<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.quartz.SchedulerTask" />
 
   <!-- Scheduler job -->
   <bean name="schedulerJob" 
     class="org.springframework.scheduling.quartz.JobDetailBean">
 
     <property name="jobClass" value="com.mkyong.quartz.SchedulerJob" />
 
     <property name="jobDataAsMap">
	 <map>
	    <entry key="schedulerTask" value-ref="schedulerTask" />
	 </map>
      </property>
   </bean>
 
   <!-- Cron Trigger, run every 10 seconds -->
   <bean id="cronTrigger"
	class="org.springframework.scheduling.quartz.CronTriggerBean">
 
	<property name="jobDetail" ref="schedulerJob" />
	<property name="cronExpression" value="0/10 * * * * ?" />
 
   </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>

5. Struts 2 + Spring

To integrate Struts 2 + Spring, just put the org.springframework.web.context.ContextLoaderListener listener class in the web.xml file.

File : web.xml


<!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>
  <display-name>Struts 2 Web Application</display-name>
  
  <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>
	  org.springframework.web.context.ContextLoaderListener
	</listener-class>
  </listener>
  
</web-app>

6. Demo

When Strut2 is started, it calls Spring and run the defined Quartz’s job – call SchedulerTask.printSchedulerMessage() at every 10 seconds.


INFO: ... initialized Struts-Spring integration successfully
16 Julai 2010 12:51:38 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
16 Julai 2010 12:51:38 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
16 Julai 2010 12:51:38 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/21  config=null
16 Julai 2010 12:51:38 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2110 ms
Struts 2 + Spring + Quartz ......
Struts 2 + Spring + Quartz ......
Struts 2 + Spring + Quartz ......
Struts 2 + Spring + Quartz ......
Struts 2 + Spring + Quartz ......

Download Source Code

References

  1. Struts 2 + Spring integration example
  2. Struts 2 Spring plugin documentation
  3. Struts + Spring integration example
  4. Struts + Quartz integration example
  5. Struts + Spring + Quartz 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
7 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
HwjohnsonjrveplavStruts 2 + Spring 3 + Quartz 1.8 Scheduler ExampleAmolSpring Tutorial Recent comment authors
newest oldest most voted
trackback
Struts 2 + Quartz scheduler integration example

[…] Struts 2 didn’t comes with any ready “Struts2-Quartz.jar” plugin, the hack is using a standard Servlet Listener to link both frameworks together. See the relationship Struts 2 <– (Listener)–> Quartz <—> Scheduler task The Spring framework has more clean and elegant solution to integrate the Struts 2 and Quartz together, check this Struts2 + Spring + Quartz integration tutorial. […]

trackback
Struts 2 Tutorials

[…] Struts 2 + Spring + Quartz scheduler integration example Integrate Struts 2, Spring and Quartz scheduler framework. […]

trackback
Spring Tutorial

[…] together.Wicket + Spring integration example Example to integrate Wicket with Spring framework.Struts 2 + Spring + Quartz scheduler integration example Example to integrate Spring + Struts 2 + Quartz.Struts 2 + Spring + Hibernate integration example […]

Amol
Guest
Amol

This is helpful but does not work with latest jars i.e 2.x, can you update this with new post.

Thanks

trackback
Struts 2 + Spring 3 + Quartz 1.8 Scheduler Example

[…] integration is similar with this Spring 2.5.6 + Quartz 1.6, just the Spring3′s dependencies are a bit messy, read Maven pom.xml below for all the […]

veplav
Guest
veplav

This was very helpful, thanks and keep up the great help you are doing to the developer community.

Hwjohnsonjr
Guest
Hwjohnsonjr

Thank You for all of your post they’re a constant source of much needed information.