Struts 1 + Quartz 2 scheduler integration example

In this tutorial, we will show you how to create a simple web application project with Apache Struts 1.x, and integrate with Quartz scheduler framework. During project start up, it will start the Quartz engine and fire the defined scheduler task.

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

Tools used :

  1. Apache Struts 1.3.10
  2. Quartz 2.1.5
  3. Eclipse IDE
  4. Maven
Note
Please visit this Quartz 2 scheduler example to learn the basic usage of the Quartz framework.

1. Dependency

Get the Struts and Quartz dependencies via Maven.

File : pom.xml


	<dependency>
		<groupId>org.apache.tomcat</groupId>
		<artifactId>servlet-api</artifactId>
		<version>6.0.35</version>
	</dependency>

	<!-- Struts 1.3 framework -->
	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts-core</artifactId>
		<version>1.3.10</version>
	</dependency>

	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts-taglib</artifactId>
		<version>1.3.10</version>
	</dependency>

	<dependency>
		<groupId>org.apache.struts</groupId>
		<artifactId>struts-extras</artifactId>
		<version>1.3.10</version>
	</dependency>

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

2. Quartz Scheduler Job

Create a Quartz job to print a message.

File : SchedulerJob.java


package com.mkyong.common.quartz;
 
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class SchedulerJob implements Job
{
	public void execute(JobExecutionContext context)
	throws JobExecutionException {
 
	   System.out.println("Print Print Struts 1.3 + Quartz 2.1.5 integration example ~");
		
	}
}

3. Struts Plug-in

Create a simple Struts plug-in and integrate the Quartz frameworks. In this plugin, during the Struts initialization, the init() method will be called and start the Quartz scheduler Job every 5 seconds.

File : QuartzPlugin.java


package com.mkyong.common.plugin;

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.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.mkyong.common.quartz.SchedulerJob;

public class QuartzPlugin implements PlugIn {

	@Override
	public void destroy() {
		// null
	}

	@Override
	public void init(ActionServlet servlet, ModuleConfig config)
			throws ServletException {

		JobDetail job = JobBuilder.newJob(SchedulerJob.class)
			.withIdentity("anyJobName", "group1").build();

		try {

			Trigger trigger = TriggerBuilder
				.newTrigger()
				.withIdentity("anyTriggerName", "group1")
				.withSchedule(
					CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
				.build();

			Scheduler scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.start();
			scheduler.scheduleJob(job, trigger);

		} catch (SchedulerException e) {
			e.printStackTrace();
		}

	}

}

4. Declares The Plugin

Include above Struts plug-in into the Struts configuration file (struts-config.xml).

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>

	<plug-in className="com.mkyong.common.plugin.QuartzPlugin" />

</struts-config>

5. Demo

During Struts startup, fire the Quartz scheduler task.

struts and quartz 2 example

Download Source Code

Download it – Struts-Quartz-2-Example.zip (24 KB)

References

  1. Quartz Official Website
  2. Quartz 2 scheduler 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
13 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
12 Comment authors
HTnaveenpallaviShreekantwebdevmvc Recent comment authors
newest oldest most voted
HT
Guest
HT

BTW, you need to save the Scheduler in an instance variable and call scheduler.shutdown() from the plugin’s destroy() method. Otherwise, when struts decides to reinitialize the plugin( for example during a redeployment) , you will end up running multiple schedulers.

naveen
Guest
naveen

How to run this project you mean by run on server. I did that but it doesn’t work

naveen
Guest
naveen

Hi mkyong. im getting following exception while running the application

May 04, 2015 3:18:04 PM org.apache.catalina.core.StandardContext loadOnStartup

SEVERE: Servlet /Struts1HelloWorld threw load() exception

javax.servlet.UnavailableException: org/quartz/SchedulerException

at org.apache.struts.action.ActionServlet.init(ActionServlet.java:402)

at javax.servlet.GenericServlet.init(GenericServlet.java:160)

at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)

at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)

at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)

at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)

at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

at java.util.concurrent.FutureTask.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source).

Please let me know how to solve it??

pallavi
Guest
pallavi

Hi MKyong,

I am using Quartz Scheduler with spring and i am getting QuartzSchedulerThread – batch acquisition of 0 triggers and DEBUG logs are flooding in the console.How can i stop it.

webdevmvc
Guest
webdevmvc

thanks a lot man., you have saved my day. been working on my obsolote java web, this method has been working very well.

blankfield
Guest
blankfield

First of all. Thanks for this tutorial.

But every 5s scheduler execute job twice.

Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:35
Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:35
Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:40
Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:40
Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:45
Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:45
Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:50
Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:50
Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:55
Struts 1.3.5 + Quartz 2.1.7 integration – 20130430 19:45:55

How can I fix it?

blankfield
Guest
blankfield

Sorry mkyong.
I solved problem.

I modified Tomcat server.xml configuration
and It works fine.

Shreekant
Guest
Shreekant

Hi,
What changes you made in tomcat server.xml to prevent scheduler execute job twice?
Please share. Thanks

Yom scor
Guest
Yom scor

Hi mkyong –
It is working fine. But DEBUG logs are flooding in the console.

13:58:06.975 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSched
ulerThread – batch acquisition of 0 triggers
13:58:26.657 [MyScheduler_QuartzSchedulerThread] DEBUG o.quartz.core.QuartzSched
ulerThread – batch acquisition of 0 triggers

Is there a way to turn off the quartz logs?

pallavi
Guest
pallavi

Hi,how did you solve this problem.I am facing the same problem now.

avinash
Guest
avinash

thank u i was searching the same ..

Karl Larsson
Guest
Karl Larsson

How can I access the datasource I use in my Action-classes?

Shyam
Guest
Shyam

Once i start this project, the scheduler is not starting…Please help

Shyam
Guest
Shyam

Sorry….Its working fine….Jar files were missing…

trackback
Struts 2 + Quartz scheduler integration example

[…] Struts + Quartz integration example […]

Denis
Guest
Denis

Thank you mkyong for the tutorial, working like a charm

trackback
Struts 2 + Spring + Quartz scheduler integration example

[…] Struts + Quartz integration example […]

trackback
Struts Tutorials | J2EE Web Development Tutorials

[…] Struts + Quartz scheduler integration Example to integrate the Struts with Quartz framework. […]