Another way to use Quartz in JBoss

I was explaining here how to create a Quartz job using the standard Quartz-ra service which comes bundled with JBoss. The method is rather limited, or I was not able to find the proper documentation on how to register new jobs on the fly. This is why in this post I will show how to access the quartz scheduler directly.

Note: everything here applies to JBoss 4.2.2GA

Remove the existing Quartz

You need to remove the existing Quartz-ra and quartz.lib

<delete file="${jboss.server}/deploy/quartz-ra.rar" />
<delete file="${jboss.server}/lib/quartz.jar" />

Install the Quartz replacement

You need to

  • copy lib/quartz-1.6.0.jar and lib/quartz-jboss-1.6.0.jar to ${jboss.server}/lib and copy the quartz service definition to ${jboss.server}/deploy.
  • edit the service definition database configuration
  • create the database tables which are required to store the Quartz state (docs/dbTables/tables_oracle.sql from the standard Quartz distribution)

At this point if you start JBoss you should see your Quartz MBean service

J2EEApplication=null,J2EEServer=Local,ServiceModule=quartz-service.xml,j2eeType=MBean,name=user:service=QuartzService,name=QuartzService

and JNDI name (using the JNDIView > list.

Access Quartz from your code

From your web servlet (as it was my case) you can access Quartz in the following way:

I was explaining here how to create a Quartz job using the standard Quartz-ra service which commes bundled with JBoss. The method is rather limited, or I was not able to find the proper documentation on how to register new jobs on the fly. This is why in this post I will show how to access the quartz scheduler directly.

Note: everything here applies to JBoss 4.2.2GA

Remove the existing Quartz

You need to remove the existing Quartz-ra and quartz.lib

<delete file="${jboss.server}/deploy/quartz-ra.rar" />
<delete file="${jboss.server}/lib/quartz.jar" />

Install the Quartz replacement

You need to

  • copy lib/quartz-1.6.0.jar and lib/quartz-jboss-1.6.0.jar to ${jboss.server}/lib and copy the quartz service definition to ${jboss.server}/deploy.
  • edit the service definition database configuration
  • create the database tables which are required to store the Quartz state (docs/dbTables/tables_oracle.sql from the standard Quartz distribution)

At this point if you start JBoss you should see your Quartz service and JNDI name.

Access Quartz from your code

From your web servlet (as it was my case) you can access Quartz in the following way:

InitialContext ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup("Quartz");

At this point you should be able to use all the Quartz API as you like.

Classloading notes

1. If you want for packaging reasons to store your Job definition (class) or make it access other classes in a ear and not the the ${jboss.server}/lib you need to make sure that your Quartz service starts (is deployed) after your ear. You can to this by editing the service definition:

<server>
  <mbean code="org.quartz.ee.jmx.jboss.QuartzService"
      name="user:service=QuartzService,name=QuartzService">
...
        <depends>jboss.j2ee:service=EARDeployment,url='xxx.ear'</depends>
...

2. If you for packaging reasons to store you Job definition (class) or make it access other classes in a war then you should make sure that you understand the JBoss unified classloader which is completely unified now except for the tomcat part. In order to unify it completely then change the line:

<attribute name="UseJBossWebLoader">false</attribute>

in ${jboss.server}/deploy/jboss-web.deployer/META-INF/jboss-service.xml.

Note: ${jboss.server} = ${your jboss path}/server/${your server name}

5 Responses

  1. It would be great if you could include the “service definition database configuration” as well. Without that, it’s hard to see the whole picture including data sources.

  2. What do you mean? Quartz has database scripts for various databases, I used the oracle ones.

  3. I used the database definition from quartz, nothing changed in the database.

  4. yes a sample for “service definition database configuration” in quartz-service.xml will be great

  5. What I mean by “edit the service definition database configuration” is to edit the value of:

    org.quartz.dataSource.QUARTZ.jndiURL = to point to my database for example:

    org.quartz.dataSource.QUARTZ.jndiURL = java:/MyOracleDS
    org.quartz.dataSource.QUARTZ_NO_TX.jndiURL = java:/MyOracleDS

Leave a Reply

*