Quartz in Sakai

Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE 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 or EJBs. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.

Configure a Quartz job:

1. Click on the Quartz Scheduler tool (in the left nav tool list).
2. Click on Jobs (at the top of the pane).
3. Click on New Job.
4. Enter a Job Name.
5. Select a job from the pop up list.
6. Click the "Post" button.
7. In the new job listing, click on the Triggers link.
8. Click on "New Trigger".
9. Create a Trigger Name.
10. Create a Cron Expression. Click the "help" link next to this field for cron expression information. (This "0 0 0/1 * * ?" means fire every hour.)
11. Click on "Post".
12. Click on "Event Log" to watch for your jobs to fire.
(The supplied test jobs write execute or execute2 to standard out.)

To create custom jobs:

There are two methods to create your own jobs. The first and recommended way is to create your jobs and register them with the Quartz Scheduler. This way they can be deployed outside of the jobscheduler component itself, in another tool or source package.

The second method is to modify the jobscheduler source itself to include your own job and modify the existing components.xml to add an entry for your job. Whilst this method may sound easiest at first, you will need to remember to maintain these modifications across upgrades.

Method 1: Creating jobs externally and registering them with the jobscheduler:

This is the preferred method as it will mean less headaches for upgrades - you can create your own package and just drop it into Sakai, rather than having to tinker with the jobscheduler project each time. It also means that any Sakai tool could, theoretically, bundle their own jobs and expose them in the jobscheduler project automatically.

To get started, check-out the following bundle into the root of your Sakai source code:

svn co https://source.sakaiproject.org/contrib/lancaster.ac.uk/quartz-example/

It's all ready to go with an example HelloWorld job so all you now need to do is build it:

mvn clean install sakai:deploy

If you get a build failure, you might want to check the pom.xml in the root of the quartz-example to make sure its version number (currently M2) matches the rest of your Sakai tools.

If it all builds fine, restart Sakai, login as an admin user and in the Admin Workspace, configure your Job to run as per the notes above. If you tail catalina.out, and run this job immediately, you can see it outputting a friendly message.

Now you should have a look at the various parts of this package and see how they wire themselves together and start writing your own job!
Specifically:

  • quartz-example/scheduler-jobs/src/java/uk/ac/lancs/e_science/jobs/HelloWorld.java for the job
  • quartz-example/pack/src/webapp/WEB-INF/components.xml for the registration beans
  • quartz-example/scheduler-jobs/pom.xml for any dependencies you might need to include ie Sakai API's
Method 2: Modifying the existing jobscheduler to include your own jobs:

1. Use the sample job classes as examples. They're in:

SAKAI-SRC/scheduler/scheduler-component-shared/src/java/org/sakaiproject/component/app/scheduler/jobs

A quartz job is just a class that implements the Job interface. It needs only one method, execute:

package org.quartz;

public interface Job {

public void execute(JobExecutionContext context)
throws JobExecutionException;
}

Oncourse uses many quartz jobs, feel free to use them as examples:

https://source.sakaiproject.org/svn/oncourse/trunk/src/jobscheduler/scheduler-component-shared/src/java/org/sakaiproject/component/app/scheduler/jobs/

Also opensymphony has a tutorial on quartz:
http://www.opensymphony.com/quartz/wikidocs/Tutorial.html

2. After creating your job class, edit:

SAKAI-SRC/scheduler/scheduler-component/src/webapp/WEB-INF/components.xml

and add your job class to the existing qrtzJobs property:

Examples:

<property name="qrtzJobs">
  <set>
    <value>org.sakaiproject.component.app.scheduler.jobs.TestJob</value>
    <value>org.sakaiproject.component.app.scheduler.jobs.TestJob2</value>
    <value>org.sakaiproject.component.app.scheduler.jobs.LongTestJob</value>
    <value>org.sakaiproject.component.app.scheduler.jobs.YourCustomJob</value>
  </set>
</property>

3. Redeploy the Scheduler tool and then following the instructions above on how to configure the Job Scheduler to run your job.

To see the old method of integrating Quartz with Sakai, see this article: Quartz Integration

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Jul 20, 2006

    casey dunn says:

    Some notes the quartz.properties file is used as of 2.1.2 if you've been upgr...

    Some notes -

    • the quartz.properties file is used as of 2.1.2
    • if you've been upgrading the Sakai databases using the 'stock' scripts during upgrades you may of missed the need to update the QRTZ_TRIGGERS table.
    • I am finding the timing of trigger execution to be rather jittery. YMMV. We're using oracle, and some of the things I've read online lead me to think it can be tightend up.
    • there are some syntax oddities in the cron strings in the version of Quartz being used. the last 2 characters must be a mix of " * ? " or " ? * " and not " * * " neat eh?
      • note to self: patch up SchedulerTool.java to log problems parsing cron strings.
  2. Jan 25, 2007

    James Watkin says:

    My other responsibilities have prevented me from being involved with Sakai/Quart...

    My other responsibilities have prevented me from being involved with Sakai/Quartz development for a long time now. My last dev experience was with Sakai 2.1.1. Another author should feel free to rewrite all of my work on this page and remove any references of credit to my name.

    Before I go, I would also like to mention a useful feature. You can (or at least could in the version I used) pass values to your Quartz job via the job and trigger names specified in the Quartz tool GUI:

    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
      try {
        LOG.debug("Job Name: " + jobExecutionContext.getJobDetail().getName());
        LOG.debug("Trigger Name: " + jobExecutionContext.getTrigger().getName());
      }
      catch (Exception exception) {
        // ToDo: Look for a way to send this message to the Quartz event log. 		}
      }
    }
  3. Sep 05

    Steve Swinsburg says:

    Ok I've tidied up the above notes and am in the process of adding the additional...

    Ok I've tidied up the above notes and am in the process of adding the additional method of registering external jobs with the scheduler.