Repository Scheduler - BloomReach Experience - Open Source CMS

This article covers a Hippo CMS version 11. There's an updated version available that covers our most recent release.

04-07-2016

Repository Scheduler

Hippo Repository exposes a scheduling service that can be used to schedule jobs with the repository. To schedule a job you can either programatically leverage the RepositoryScheduler API interface, or configure the job manually in the repository.

Leveraging the RepositoryScheduler API

The API of the repository scheduler service is located in the repository api module. You also need the Hippo services dependency:

 

<dependency>
  <groupId>org.onehippo.cms7</groupId>
  <artifactId>hippo-services</artifactId>
</dependency>
<dependency>
  <groupId>org.onehippo.cms7</groupId>
  <artifactId>hippo-repository-api</artifactId>
</dependency>

To obtain an instance of the repository scheduler service, get it from the hippo service registry:

 

final RepositoryScheduler scheduler =
           HippoServiceRegistry.getService(RepositoryScheduler.class);

To schedule a job using the scheduler you need to provide a job description in the form of a RepositoryJobInfo object and a specification of when to run the job in the form of a RepositoryJobTrigger:

 

final RepositoryJobInfo myJobInfo = new RepositoryJobInfo("my",
                                                   TestRepositoryJob.class);
testJobInfo.addAttribute("foo", "bar");
final RepositoryJobTrigger myJobTrigger =
                            new RepositoryJobSimpleTrigger("my", new Date());

scheduler.scheduleJob(myJobInfo, myJobTrigger);

The job to be scheduled ( TestRepositoryJob in the example above) must have a public no-argument constructor and must be an instance of interface org.onehippo.repository.scheduling.RepositoryJob which has one method:

 

/**
 * Job execution callback.
 *
 * @param context operational context object.
 * @throws RepositoryException when an error occurs.
 */
public void execute(RepositoryJobExecutionContext context)
                                               throws RepositoryException;

 

From the passed-in context you can obtain a session by calling:

 

context.createSession(new SimpleCredentials("myuser", "mypass".toCharArray());

 

Or a system session by calling:

 context.createSystemSession();

These sessions must be logged out after you are done using them.

Attributes that were passed in to the RepositoryJobInfo with which this RepositoryJob was scheduled are also available from the context:

 

assert context.getAttribute("foo").equals("bar");

 

Available Triggers

There are two different implementations of RepositoryJobTrigger at your disposal: RepositoryJobSimpleTrigger and RepositoryJobCronTrigger.

If you want to schedule a job to execute once at a give date and time, you should use the former of these two:

 

final RepositoryJobTrigger myJobTrigger =
                     new RepositoryJobSimpleTrigger("my", new Date());

 

This trigger can also be constructed with the optional repeatCount and repeatInterval arguments. The job will then be scheduled to execute a total of repeatCount times starting at the given startDate. The repeatInterval argument specifies the number of milliseconds between job executions.

The second trigger at you disposal triggers the execution of jobs based on a cron expression:

final RepositoryJobTrigger myJobTrigger =
                   new RepositoryJobCronTrigger("my","0 15 10 ? * 6L");

If you use the scheduler from a DaemonModule make sure your module is loaded after the module that registers the scheduler by leveraging the @RequiresService annotation described there.

Configuring jobs manually

Instead of registering a job programatically with the RepositoryScheduler service, you can add it directly to the repository. Repository jobs are persisted at /hippo:configuration/hippo:modules/scheduler/hippo:moduleconfig/. Below a job group of your own choosing add a job node with an appropriate trigger: 

<sv:node sv:name="example" xmlns:sv="http://www.jcp.org/jcr/sv/1.0">
  <sv:property sv:name="jcr:primaryType" sv:type="Name">
    <sv:value>hipposched:repositoryjob</sv:value>
  </sv:property>
  <sv:property sv:multiple="true" sv:name="hipposched:attributeNames" sv:type="String">
    <sv:value>key</sv:value>
  </sv:property>
  <sv:property sv:multiple="true" sv:name="hipposched:attributeValues" sv:type="String">
    <sv:value>value</sv:value>
  </sv:property>
  <sv:property sv:name="hipposched:repositoryJobClass" sv:type="String">
    <sv:value>org.example.ExampleRepositoryJob</sv:value>
  </sv:property>
  <sv:node sv:name="hipposched:triggers">
    <sv:property sv:name="jcr:primaryType" sv:type="Name">
      <sv:value>hipposched:triggers</sv:value>
    </sv:property>
    <sv:node sv:name="example">
      <sv:property sv:name="jcr:primaryType" sv:type="Name">
        <sv:value>hipposched:crontrigger</sv:value>
      </sv:property>
      <sv:property sv:name="hipposched:cronExpression" sv:type="String">
        <sv:value>0 0/5 * * * ?</sv:value>
      </sv:property>
    </sv:node>
  </sv:node>
</sv:node>

Notice how you can parameterize the RepositoryJob using the two properties hipposched:attributeNames and hipposched:attributeValues. The key/value pairs are available as described above from the RepositoryJobExecutionContext. This configuration schedules a job using a trigger that fires every 5 minutes. You can add as many triggers as you need.

Disabling jobs and triggers

Job and trigger nodes have a property called hipposched:enabled that can be used to disable the job or trigger in question. If the property is not specified, it is interpreted as that the job or trigger is enabled.

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?