Wednesday, March 3, 2010

Seam and Quartz integration

Seam is a great framework for easily building Internet applications in Java. It integrates with lots of well known existing java technologies and serves as a complement to JSF (Java Server Faces).
Quartz is a job scheduling framework allowing to easily add the capability of running scheduled jobs in your applications.
Quartz can be easily integrated with Seam. By integrating both technologies you will be able to enjoy Seam approach to managing beans on your web application.
Let’s take a look at a simple example:
First make sure you have all the jars needed to run Quartz and Seam.
Then go to your Seamcomponents.xml” file and add these lines:
<async:quartz-dispatcher/>
  <event type="org.jboss.seam.postInitialization">
  <action execute="#{quartzController.scheduleTimer}"/>
</event>
Now, put in your sources root directory (usually the “src”) the file: “seam.quartz.properties” file. A typical file may look like this:
org.quartz.scheduler.instanceName = Sched1
org.quartz.scheduler.instanceId = 1
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
Now, to the Java code. The code is constructed from 2 Seam beans. The first bean contains the Quartz trigger class. I looks like:
package com.bashan.blog.job;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.async.QuartzTriggerHandle;
import java.io.Serializable;
import java.util.Date;
/**
* @author Bashan
*/
@Name("quartzController")
@AutoCreate
public class QuartzController implements Serializable {
@In
ScheduleProcessor processor;
private QuartzTriggerHandle quartzTriggerHandleDoJob;
public void scheduleTimer() {
quartzTriggerHandleDoJob = processor.doJob(new Date(), "0 0/1 * * * ?");
}
}
Note, that when calling to “doJob” method, a cron expression is passed. This expression describes the frequency in which the job will run (event minute in the example).

The second class contains the actual task we would like to do:
package com.bashan.blog.job;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.async.Asynchronous;
import org.jboss.seam.annotations.async.Expiration;
import org.jboss.seam.annotations.async.IntervalCron;
import org.jboss.seam.async.QuartzTriggerHandle;
import org.jboss.seam.log.Log;
import java.util.Date;
/**
* @author Bashan
*/
@Name("processor")
@AutoCreate
@Scope(ScopeType.APPLICATION)
public class ScheduleProcessor {
 
  @Logger
  static Log log;
 
  @Asynchronous
  public QuartzTriggerHandle doJob(@Expiration Date when, @IntervalCron String interval) {
    System.out.println("I am a Quartz job.");
    log.debug("Pass some information to the log");
    return null;
  }
}
The method “doJob” is a Seam asynchronous method. This method will be executed according to the data of the cron expression given in the class: QuartzController.

1 comment: