JBoss Quartz tutorial

User Rating: 4 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Inactive

 1) Standalone Quartz server:

This way you are responsible of starting/stopping your own Quartz scheduler. This is the simpler way and could be used for smaller projects :   

package sample; 
import java.util.Date;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzServer {
 public static void main(String[] args) {
 QuartzServer server = new QuartzServer();
 try {
 } catch (SchedulerException ex) {
 protected void startScheduler() throws SchedulerException {
 SchedulerFactory sf=new StdSchedulerFactory();
 Scheduler sched=sf.getScheduler();
 JobDetail jd=new JobDetail("myjob",sched.DEFAULT_GROUP,TestJob.class);
 SimpleTrigger st=new SimpleTrigger("mytrigger",sched.DEFAULT_GROUP,new Date(),
 sched.scheduleJob(jd, st);

Here the 'default' scheduler -defined in "quartz.properties"- is retreived from the SchedulerFactory and a Job is enlisted to fire every 60 seconds. (The TestJob class is exposed at the beginning of this article) 

2) Embed Quartz inside an application server

Larger projects may as well use Quartz embedded inside the container. This leads to a lot of advanced features like job clustering which we'll explore in the next articles.
JBoss comes already with some Quartz libraries. Anyway I suggest you to download the Quartz libraries from the home page:

Let's clean the house!

If you want to patch correctly Quartz, at first remove the Quartz libraries under server/lib and replace them with quartz-1.8.X.jar and quartz-jboss-1.8.X.jar (For this sample we're using Version 1.8.4 (latest stable release). Remove as well "quartz-ra.rar" in the "deploy" folder.

Add quartz service to jboss

In order to register Quartz Service on JBoss you need to register its Mbean. In the Quartz distribution you can find a couple of samples of quartz-service.xml. Here's a simple one, we'll discuss more in depth about it later...

<?xml version="1.0" encoding="UTF-8"?> <server> 
 <mbean code="org.quartz.ee.jmx.jboss.QuartzService" 
 <attribute name="JndiName">Quartz</attribute> 
 <attribute name="Properties">        
 org.quartz.scheduler.instanceName = DefaultQuartzScheduler 
 org.quartz.scheduler.rmi.export = false 
 org.quartz.scheduler.rmi.proxy = false 
 org.quartz.scheduler.xaTransacted = false 
 org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  
 org.quartz.threadPool.threadCount = 5 
 org.quartz.threadPool.threadPriority = 4 
 org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore  

Ok so now we're ready to rock!

This is a sample Servlet doGet method which looks up the Scheduler registered in the JNDI tree and schedules a new Job that simply fires every 5 minutes.

 public void doGet(HttpServletRequest request, HttpServletResponse response)

 throws ServletException, IOException {
 try {
 InitialContext ctx = new InitialContext();
 StdScheduler scheduler = (StdScheduler) ctx.lookup("Quartz");

 JobDetail jd=new JobDetail("myjob",scheduler.DEFAULT_GROUP,TestJob.class);
 CronTrigger ct = new CronTrigger("cronTrigger","group2",""0 0/5 * * * ?");

 } catch (Exception exc) {  


Hint: have you got a class cast exception when you lookup the StdScheduler ? I guess you have added Quartz libraries under WEB-INF/lib : remove them from there !


JBoss CheatSheet for Admins