Page 1 of 2

It's time to learn new
Quartz 2 API using
JBoss AS 7. This tutorial shows how to install it on JBoss AS 7 and how to configure some simple Jobs of it. In the second part of it, we will show how to add an advanced configuration which includes a JDBC Job Store.
Well the first thing you need is grabbing a Quartz 2.X release from http://quartz-scheduler.org/downloads
(In this tutorial we will use Quartz 2.0.2 although a Quartz 2.1 release has been just published.)
In the earlier versions of JBoss AS 7 Quartz was bundled along with the application server, now we will set up by ourselves: however don't worry it will take just a minute.
<
Installing quartz module on JBoss AS 7
Create the following module structure under JBOSS_HOME/modules
+---org
+----quartz
+-----main
module.xml
quartz-all-2.0.2.jar
And here's module.xml file:
<module xmlns="urn:jboss:module:1.1" name="org.quartz">
<resources>
<resource-root path="quartz-all-2.0.2.jar"/>
<!-- Insert resources here -->
</resources>
<dependencies>
<module name="org.slf4j"/>
<module name="javax.api"/>
</dependencies>
</module>
As you can see the module contains 2 dependencies, although javax.api dependency will be just needed if you want to store your jos through a Datasource JDBC connection.Ok, now let's create a sample web application with a basic Quartz servlet in it:
package com.sample;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.quartz.*;
import org.quartz.impl.*;
import static org.quartz.JobBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.DateBuilder.*;
@WebServlet("/Test")
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// step 1
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
sched.start();
Date runTime = evenMinuteDate(new Date());
// Trigger the job to run on the next round minute
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startAt(runTime)
.build();
// Define job instance
JobDetail job1 = newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
// Schedule the job with the trigger
sched.scheduleJob(job1, trigger);
} catch ( Exception de) {
throw new IOException(de.getMessage());
}
}
}
And here's the simple HelloJob class:
package com.sample;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job {
@Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("Hello World! - " + new java.util.Date());
}
}
The most evident for you at the moment are Quartz static initializers. Quartz 2.0 API provides a new builder-based API based on a Domain Specific Language (DSL) for constructing job and trigger definitions. Usage of static imports makes your code nice and clean when using the new DSL.
Here's for example how you used to create a JobDetail in older quartz Api:
JobDetail job = new JobDetail("myJob", "myGroup");
job.setJobClass(MyJobClass.class);
job.getJobDataMap().put("someKey", "someValue");
and here's the Quartz 2.0 version:
JobDetail job = newJob(MyJobClass.class)
.withIdentity("myJob", "myGroup")
.usingJobData("someKey", "someValue")
.build();