Thursday, January 31, 2008

Implementing Quartz to Struts

Our Objective is we are going to implement Quartz to Struts Framework.

Anyway what is Quartz?
- Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components or EJBs. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.(

We are going to make an mail scheduler attached in our web application using the Struts framework. Quartz is pretty simple and straight forward, All we need to do is implement the Job (org.quartz.Job) and instantiate the implementation. After doing that Quartz stuff we will implement the Struts Plugin (org.apache.struts.action.PlugIn) and will map the our made plugin to Struts-config.xml.

lets get started.

1. We'll make an Java class that will implement the Job (org.quartz.Job).
sample below.

import Constants;
import MailDetails;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

* @author amontejo
public class MailNewsletterJob implements Job {

public void execute(JobExecutionContext context) throws JobExecutionException {
Connection con = null;
try {
} catch (ClassNotFoundException c) {
System.out.println("Class not found!");
try {
String strUser = Constants.USER;
String strPassword = Constants.password;
String strDatabase = Constants.DATABASE;
String strServer = Constants.SERVER;
int mysqlport = Constants.PORT;
String url = "jdbc:mysql://" + strServer + ":" + mysqlport + "/" + strDatabase;
Properties props = new Properties();
props.setProperty("user", strUser);
props.setProperty("password", strPassword);
try {
con = DriverManager.getConnection(url, props);
} catch (SQLException sqle) {

try {//email newsletter
MailDetails maildetails = new MailDetails();
mailer.postMail(con, maildetails.getRecipients(), "Sample Mail", message, "From Quartz");
} catch (Exception e) {
} catch (Exception s) {
System.out.println("Doesn't establish the connection!");

Note: MailDetails is simple POJO that implement the Javamail API.

2. We'll make an Java file that will instantiate the Job.

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

* @author amontejo
public class MailNewsletterScheduler {

public MailNewsletterScheduler() throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sche = sf.getScheduler();
JobDetail jDetail = new JobDetail("Newsletter", "SampleNewsletterJob", MailNewsletterJob.class);
//"0 0 12 * * ?" Fire at 12pm (noon) every day
CronTrigger crTrigger = new CronTrigger("cronTrigger", "SampleNewsletterJob", "0 0 12 * * ?");
sche.scheduleJob(jDetail, crTrigger);

As you can see we to instantiate an JobDetails and pass as argument the Job that we made which is the MailNewsletterJob.class, and well create an Trigger to set when our Job will start to get working.


3. Next is, we are going to create an implementation of the Struts Plugin.

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

* @author amontejo
public class NewsLetter implements PlugIn {

public static final String PLUGIN_NAME_KEY = NewsLetter.class.getName();

public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {
try {
System.out.println("Initializing NewsLetter PlugIn");
ServletContext context = null;
context = servlet.getServletContext();
MailNewsletterScheduler objPlugin = new MailNewsletterScheduler();
context.setAttribute(PLUGIN_NAME_KEY, objPlugin);
} catch (Exception ex) {

4. And Lastly we will tell and map our plugin to Struts-config.xml.

Done! As you can see its pretty straight forward, we just create an implementation of Quartz Job,
instantiate the Job Details, Create an implementation of Struts Plugin and map to Struts configuration xml. See
more about Quartz Framework.


  1. Thanks for you input over Quartz.
    could give me an example of struts-config.xml and web.xml to activate the plug in??

    My email


  2. This example is incomplete, you should have posted the struts-config.xml also. can you please email the same to

  3. Thank you for your post, Allen. It almost solves my problem. I've tried struts application with your codes but it's not working although I've made the same configuration like yours.
    I'll send my war file to you by email. Please correct for me. For now, it goes to the line before I create the JobDetail. The later never comes out and execute.

    Thanks and best regards

  4. please see

    How to activate your scheduler in struts.


  5. what about closing the scheduler one the application goes done.
    This could cause some trouble