blob: 6c9623b5eac2bbd6120de0b8c9bf8fbd16c23727 [file] [log] [blame]
:index-group: Java EE Connectors
:jbake-type: page
:jbake-status: status=published
= Quartz Resource Adapter usage
NOTE: this example is somewhat dated. It predates the schedule API which
was added to EJB 3.1. Modern applications should use the schedule API
which has many, if not all, the same features as Quartz. In fact, Quartz
is the engine that drives the `@Schedule` and `ScheduleExpression`
support in OpenEJB and TomEE.
Despite being dated from a scheduling perspective it is still an
excellent reference for how to plug-in and test a custom Java EE
Resource Adapter.
== Project structure
As `.rar` files do not do well on a standard classpath structure the
goal is to effectively ``unwrap'' the `.rar` so that its dependencies
are on the classpath and its `ra.xml` file can be found and scanned by
We do this by creating a mini maven module to represent the `.rar` in
maven terms. The `pom.xml` of the ``rar module'' declares all of the
jars that would be inside the `.rar` as maven dependencies. The `ra.xml`
file is added to the project in `src/main/resources/META-INF/ra.xml`
where it will be visible to other modules.
=== ra.xml
The connector in question has both inbound and outbound Resource
Adapters. The inbound Resource Adapter can be used to drive message
driven beans (MDBs)
the outbound Resource Adapter, `QuartzResourceAdapter`, can be injected
into any component via `@Resource` and used to originate and send
messages or events.
<connector xmlns=""
<description>Quartz ResourceAdapter</description>
<display-name>Quartz ResourceAdapter</display-name>
<eis-type>Quartz Adapter</eis-type>
<resourceadapter id="QuartzResourceAdapter">
== Using the Outbound Resource Adapter
Here we see the outbound resource adapter used in a stateless session
bean to schedule a job that will be executed by the MDB
package org.superbiz.quartz;
import org.apache.openejb.resource.quartz.QuartzResourceAdapter;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import javax.ejb.Stateless;
import javax.naming.InitialContext;
import java.util.Date;
public class JobBean implements JobScheduler {
public Date createJob() throws Exception {
final QuartzResourceAdapter ra = (QuartzResourceAdapter) new InitialContext().lookup("java:openejb/Resource/QuartzResourceAdapter");
final Scheduler s = ra.getScheduler();
//Add a job type
final JobDetail jd = new JobDetail("job1", "group1", JobBean.MyTestJob.class);
jd.getJobDataMap().put("MyJobKey", "MyJobValue");
//Schedule my 'test' job to run now
final SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1", new Date());
return s.scheduleJob(jd, trigger);
public static class MyTestJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("This is a simple test job to get: " + context.getJobDetail().getJobDataMap().get("MyJobKey"));
== Recieving data from the Inbound Resource Adapter
package org.superbiz.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "cronExpression", propertyValue = "* * * * * ?")})
public class QuartzMdb implements Job {
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("Executing Job");
== Test case
package org.superbiz.quartz;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Date;
import java.util.Properties;
public class QuartzMdbTest {
private static InitialContext initialContext = null;
public static void beforeClass() throws Exception {
if (null == initialContext) {
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
initialContext = new InitialContext(properties);
public static void afterClass() throws Exception {
if (null != initialContext) {
initialContext = null;
public void testLookup() throws Exception {
final JobScheduler jbi = (JobScheduler) initialContext.lookup("JobBeanLocal");
final Date d = jbi.createJob();
System.out.println("Scheduled test job should have run at: " + d.toString());
public void testMdb() throws Exception {
// Sleep 3 seconds and give quartz a chance to execute our MDB
== Running
Running org.superbiz.quartz.QuartzMdbTest
Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06
INFO - openejb.home = /Users/dblevins/examples/quartz-app/quartz-beans
INFO - openejb.base = /Users/dblevins/examples/quartz-app/quartz-beans
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Found ConnectorModule in classpath: /Users/dblevins/examples/quartz-app/quartz-ra/target/quartz-ra-1.0.jar
INFO - Found EjbModule in classpath: /Users/dblevins/examples/quartz-app/quartz-beans/target/classes
INFO - Beginning load: /Users/dblevins/examples/quartz-app/quartz-ra/target/quartz-ra-1.0.jar
INFO - Extracting jar: /Users/dblevins/examples/quartz-app/quartz-ra/target/quartz-ra-1.0.jar
INFO - Extracted path: /Users/dblevins/examples/quartz-app/quartz-ra/target/quartz-ra-1.0
INFO - Beginning load: /Users/dblevins/examples/quartz-app/quartz-beans/target/classes
INFO - Configuring enterprise application: /Users/dblevins/examples/quartz-app/quartz-beans/classpath.ear
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean JobBean: Container(type=STATELESS, id=Default Stateless Container)
INFO - Configuring Service(id=QuartzResourceAdapter, type=Resource, provider-id=QuartzResourceAdapter)
INFO - Configuring Service(id=quartz-ra-1.0, type=Container, provider-id=Default MDB Container)
INFO - Enterprise application "/Users/dblevins/examples/quartz-app/quartz-beans/classpath.ear" loaded.
INFO - Assembling app: /Users/dblevins/examples/quartz-app/quartz-beans/classpath.ear
INFO - Jndi(name=JobBeanLocal) --> Ejb(deployment-id=JobBean)
INFO - Jndi(name=global/classpath.ear/quartz-beans/JobBean!org.superbiz.quartz.JobScheduler) --> Ejb(deployment-id=JobBean)
INFO - Jndi(name=global/classpath.ear/quartz-beans/JobBean) --> Ejb(deployment-id=JobBean)
INFO - Created Ejb(deployment-id=JobBean, ejb-name=JobBean, container=Default Stateless Container)
INFO - Created Ejb(deployment-id=QuartzMdb, ejb-name=QuartzMdb, container=quartz-ra-1.0)
Executing Job
INFO - Started Ejb(deployment-id=JobBean, ejb-name=JobBean, container=Default Stateless Container)
INFO - Started Ejb(deployment-id=QuartzMdb, ejb-name=QuartzMdb, container=quartz-ra-1.0)
INFO - Deployed Application(path=/Users/dblevins/examples/quartz-app/quartz-beans/classpath.ear)
This is a simple test job to get: MyJobValue
Scheduled test job should have run at: Fri Oct 28 17:05:12 PDT 2011
Executing Job
Executing Job
Executing Job
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.971 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0