Monday, May 31, 2010

Quartz Task Scheduling on JBOSS 4.2.2

I've been working on a J2EE app which needed to run a specific task at 8.00 AM every morning. I've used Quartz as a scheduler with Spring on Weblogic, but I'm not using Spring on my current project so I was probably going to have to use Quartz raw. That is, until I read that JBOSS has Quartz built in. This has been handled as a resource adapter. That's the quartz-ra.rar file.

How to use this goodness, is to create a Message driven bean (MDB). You can do this by creating a class that extends the org.quartz.Job interface. See an example here.

Notice that I have commented out the annotations just above the class definition as I believe those are deployment specific decisions hence would prefer to make those declarations in configuration files. If you're not as picky you could uncomment them. Note that the @ResourceAdapter annotation is JBOSS specific and would impact negatively on the portability of your code!

In your ejb-jar.xml you define the class as an EJB. See example here.

If you're following my advice and not using proprietary annotations, you'll need to create a jboss.xml file like this.

The jboss.xml file creates the link between the EJB and the resource adapter while the MDB definition in the ejb-jar.xml file. Be careful with the cronTrigger property as it does not behave in the exact same manner that cron does in UNIX (Cron on Wikipedia). There is an additional position on the left for seconds. Follow the Quartz Scheduler tutorial here. Deploy and enjoy!