Pages

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.(http://www.opensymphony.com/quartz/)


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;

/**
*MailNewsletterJob.java
* @author amontejo
*/
public class MailNewsletterJob implements Job {

public void execute(JobExecutionContext context) throws JobExecutionException {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} 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) {
sqle.printStackTrace();
}

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

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;

/**
*MailNewsletterScheduler.java
* @author amontejo
*/
public class MailNewsletterScheduler {

public MailNewsletterScheduler() throws Exception {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sche = sf.getScheduler();
sche.start();
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;

/**
*NewsLetter.java
* @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) {
ex.printStackTrace();
}
}
}

------------------------------------------------------------------------
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 http://www.opensymphony.com/quartz/wikidocs/TutorialLesson1.html
more about Quartz Framework.

5 comments:

  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 valle.abraham@gmail.com

    Thanks.

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

    ReplyDelete
  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

    ReplyDelete
  4. please see http://javajumper.blogspot.com/2009/05/activating-quartz-unto-struts.html

    How to activate your scheduler in struts.

    thanks

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

    ReplyDelete