Overview

This Service functions as a wrapper around the Quartz Scheduler. It is written for use in any Avalon compatible container.

Usage

Scheduled jobs can be either created programmatically (using the Quartz API) or by using an XML configuration based on the 'XMLSchedulingDataProcessorPlugin'. A scheduled job consists of a class implementing the interface org.quartz.Job This requires just that one method be defined, execute(), whose org.quartz.JobExecutionContext argument provides you with details of the execution context when the task is triggered.


import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class SimpleJob implements Job
{
  public static boolean executed = false;

  public SimpleJob()
  {
    super();
  }

  public void execute(JobExecutionContext context)
    throws JobExecutionException
  {
    executed = true;
  }
}
    

In order to invoke the SimpleJob we have to tell Quartz when to invoke the 'execute' method. In this case we create a CRON trigger firing every second to invoke SimpleJob#execute() on a newly created instance. Please note that we define a set of parameters using the <job-data-map> which are available using 'JobExecutionContext'.


<schedule>
  <job>
    <name>simpleJob</name>
    <group>TURBINE</group>
    <description>A simple job</description>
    <job-class>org.apache.fulcrum.quartz.test.SimpleJob</job-class>
    <job-data-map>
      <entry>
        <key>dressing-list</key>
        <value>ketchup,mayo</value>
      </entry>
      <entry>
        <key>burger-type</key>
        <value>hotdog</value>
      </entry>
    </job-data-map>
  </job>
  <trigger>
    <cron>
      <!-- define a Cron trigger firing every second -->
      <name>cronTrigger</name>
      <group>TURBINE</group>
      <job-name>simpleJob</job-name>
      <job-group>TURBINE</job-group>
      <cron-expression>* * * * * ?</cron-expression>
    </cron>
  </trigger>
</schedule> 

    

Implementation Details

The implementation registers a JobListener which intercepts the execution of the job to set some Avalon infrastructure such 'Logger' and 'ServiceManager'. This allows an simple invocation of an Avalon service within the job execution.

On shutdown the service implementation ensures that all currently executed jobs are finished. This avoids problems if a scheduled job tries to access already disposed services.

The service can be started without quartz configuration (relying on default settings) and preconfigured triggers.