Monday, 28 March 2011

Scheduling a job using Jboss-5.1.0.GA and Quartz

What is Quartz?

Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components that may execute virtually anything you may program them to do. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering. Read more...

What can Quartz do for you?

If your application has tasks that need to occur at given moments in time, or if your system has recurring maintenance jobs then Quartz may be your ideal solution.

Programmatically scheduling jobs is quite straight forward and there are tons of quartz tutorials out there that can teach you how to do this.

For this post I am going to focus on how to schedule a job using xml config files.

How to create a Job

Creating a quartz job is as easy as adding the quartz-1.8.4.jar to the build path, implementing the org.quartz.Job interface in your class and overriding the execute method. That's it!

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
  //insert the method body here
}

  
Now, to schedule the job...
Quartz uses cron expressions to schedule the jobs so if you're familiar with unix cron jobs this shouldn't be too difficult. CronTrigger Tutorial 

quartz-service.xml

This file contains the configuration for the quartz scheduler and must be deployed in the JBOSS_HOME/server/default/deploy directory.
Here's what a sample quartz-service.xml file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.quartz.ee.jmx.jboss.QuartzService"  name="user:service=QuartzService,name=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
                          org.quartz.plugin.jobInitializer.class =  org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
                         org.quartz.plugin.jobInitializer.fileNames = C:/Jboss/jboss-5.1.0.GA/server/default/deploy/quartz- config.xml
                         org.quartz.plugin.jobInitializer.failOnFileNotFound = true
                         org.quartz.plugin.jobInitializer.scanInterval = 120
                         org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
                </attribute>
</mbean>
</server>


quartz-config.xml

This file contains the configuration for the jobs that need to be scheduled and it must also be deployed in the JBOSS_HOME/server/default/deploy directory.
Here's a sample quartz-config.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">

<schedule>
 <job>
           <name>HelloWorldJob</name>
           <group>MyJobGroup</group>
           <description>This is just a simple example of how to schedule a job with quartz</description>
           <job-class>za.co.greeter.HelloWorld</job-class>
           <volatility>false</volatility>
           <durability>false</durability>
           <recover>false</recover>
           <job-data-map>
                  <entry>
                            <key>greeting</key>
                            <value>Hello World</value>
                  </entry>
           </job-data-map>
 </job>
<trigger>
             <cron>
                      <name>HelloWorld-trigger</name>
                      <group>MyJob-triggers</group>
                      <job-name>HelloWorldJob</job-name>
                      <job-group>MyJobGroup</job-group>
                      <cron-expression>0 15 10 ? * *</cron-expression>
                      <!-- This job will fire at 10:15am everyday -->
              </cron>
</trigger>

</schedule>

</job-scheduling-data>


All that's left to do is to to copy the quartz-1.8.4.jar and the quartz-jboss-1.8.4.jar to the JBOSS_HOME/server/default/lib directory and restart the server.

For more about Quartz check out the Quartz website: http://www.quartz-scheduler.org/


3 comments:

  1. After reading this Blog I got full knowledge about scheduling of job. Really I like this Blog
    Job Scheduling.

    ReplyDelete
  2. Awesome post! Just what I needed.

    ReplyDelete
  3. Can I use quartz 1.8.6 on JBoss 4.2.2 GA?

    ReplyDelete