Struts 2 + Spring 3 + Quartz 1.8 Scheduler Example

In this tutorial, we will show you how to integrate Struts 2 + Spring 3 + Quartz 1.8.6.

Why NOT Quartz 2?
Currently, Spring 3 is still NOT support Quartz 2 APIs, see this SPR-8581 bug report. Will update this article again once bug fixed is released.

Tools used

  1. Spring 3.1.5.RELEASE
  2. Quartz 1.8.6
  3. Struts 2.3.4
  4. Struts2-spring-plugin 2.3.4
  5. Maven 3
  6. Eclipse 4.2

1. Project Folder

Here’s the final project folder structure.

2. Dependency Libraries

The 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 dependencies you need to develop this tutorial.

See the XML comments to understand why you need that jar.

File : pom.xml


  ...
<dependencies>

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

  <!-- Quartz framework -->
  <dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>1.8.6</version>
  </dependency>

  <!-- Spring 3 dependencies -->
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

  <!-- QuartzJobBean in spring-context-support.jar -->
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

  <!-- Struts 2 + Spring 3 need this jar, ContextLoaderListener -->
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

  <!-- Spring + Quartz need transaction -->
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-tx</artifactId>
	<version>3.1.2.RELEASE</version>
  </dependency>

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

</dependencies>
  ...

3. Spring 3 + Quartz

To integrate both Spring3 and Quartz, create a class extends org.springframework.scheduling.quartz.QuartzJobBean, and implements the executeInternal() method, just like how you create scheduler job in Quartz.

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
{
	 
	protected void executeInternal(JobExecutionContext context)
	throws JobExecutionException {
 
		System.out.println("Struts 2.3.4 + Quartz 1.8.6 + Spring 3.1.2");
 
	}
}

File : applicationContext.xml – puts the entire Quartz’s scheduler and Spring detail in applicationContext.xml. See 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-3.0.xsd">

  <!-- Spring Quartz Scheduler job -->
  <bean name="schedulerJob" class="org.springframework.scheduling.quartz.JobDetailBean">
	<property name="jobClass" value="com.mkyong.quartz.SchedulerJob" />
  </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>

  <!-- DI -->
  <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 2 + Spring 3

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

Note
For detail, please read this Struts 2 + Spring integration example.

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>

5. Demo

Done, when Strut2 is started, it call Spring and run the defined Quartz’s job.


Jul 24, 2012 4:49:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jul 24, 2012 4:49:07 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jul 24, 2012 4:49:07 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3430 ms
Struts 2.3.4 + Quartz 1.8.6 + Spring 3.1.2  //run this every 10 seconds.
Struts 2.3.4 + Quartz 1.8.6 + Spring 3.1.2
Struts 2.3.4 + Quartz 1.8.6 + Spring 3.1.2

Download Source Code

Download it – Struts2-Spring3-Quartz-Example.zip (20 KB).

References

  1. Struts 2 + Spring integration example
  2. Struts 2 Spring plugin documentation
  3. QuartzJobBean JavaDoc
  4. Spring should support Quartz 2.0 CronTrigger interface
  5. Struts 2 + Spring 2.5.6 + 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
5 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
O.BJN GerbauxAlexanderarjunStruts 2 + Spring 2.5.6 + Quartz 1.6 scheduler integration example Recent comment authors
newest oldest most voted
O.B
Guest
O.B

Hi,
Can you tell how to use Spring and quartz in a non web application ?

JN Gerbaux
Guest
JN Gerbaux

Hi,
Seems Spring 3 (and 4 ?) now supports Quartz 2. Shouldn’t you update quartz/spring articles ? ;-)

arjun
Guest
arjun

Hi,

I am working on a new project.We are deciding about the frameworks to be used.I have a dilemma to use whether struts 2 or spring mvc 3. Can you please let me know the difference between both the frameworks and in what kind of scenarios one has an edge over the other.

Alexander
Guest
Alexander

Struts2 sucks. Spring – the best

trackback
Struts 2 + Spring 2.5.6 + Quartz 1.6 scheduler integration example

[…] Eclipse 3.6 Note You may also interest at this – Struts 2 + Spring 3 + Quartz 1.8.6 integration example.. […]

trackback
Spring 3 + Quartz 1.8.6 scheduler example

[…] Struts 2 + Spring 3 + Quartz 1.8 example This article is under – Spring Tutorials , Tags: Quartz , Spring 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 : Spring MVC TutorialsJDBC TutorialsJava JSON TutorialsHibernate Tutorials […]