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 Seam “components.xml” file and add these lines:
Now, put in your sources root directory (usually the “src”) the file: “seam.quartz.properties” file. A typical file may look like this:<async:quartz-dispatcher/><event type="org.jboss.seam.postInitialization"><action execute="#{quartzController.scheduleTimer}"/></event>
Now, to the Java code. The code is constructed from 2 Seam beans. The first bean contains the Quartz trigger class. I looks like:org.quartz.scheduler.instanceName = Sched1org.quartz.scheduler.instanceId = 1org.quartz.scheduler.rmi.export = falseorg.quartz.scheduler.rmi.proxy = falseorg.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount = 3org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
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).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")
@AutoCreatepublic class QuartzController implements Serializable {@InScheduleProcessor processor;private QuartzTriggerHandle quartzTriggerHandleDoJob;
public void scheduleTimer() {quartzTriggerHandleDoJob = processor.doJob(new Date(), "0 0/1 * * * ?");}}
The second class contains the actual task we would like to do:
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.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 {@Loggerstatic Log log;
@Asynchronouspublic 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;}}
great post.
ReplyDeletehelped me a lot
thanks