Repository Scheduler

Overview

The repository in Bloomreach Content provides a scheduling service for running jobs within the repository. You can schedule jobs either by using the RepositoryScheduler API or by configuring jobs directly in the repository.

Using the RepositoryScheduler API

The repository scheduler API is available in the repository API module. Add the following dependencies to your project:

<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, retrieve it from the Hippo service registry:

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

To schedule a job, provide a job description using a RepositoryJobInfo object and specify when the job should run with 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 class (TestRepositoryJob in the example) must have a public no-argument constructor and must implement the org.onehippo.repository.scheduling.RepositoryJob interface, which defines a single method:

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

You can obtain a session from the provided context:

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

To obtain a system session, use:

 context.createSystemSession();

Always log out sessions after use.

Attributes passed to RepositoryJobInfo are accessible from the execution context:

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

Trigger Types

Two implementations of RepositoryJobTrigger are available: RepositoryJobSimpleTrigger and RepositoryJobCronTrigger.

To schedule a job to run once at a specific date and time, use RepositoryJobSimpleTrigger:

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

You can also specify optional repeatCount and repeatInterval arguments. The job will run repeatCount times, starting at the specified startDate. repeatInterval defines the interval in milliseconds between executions.

To schedule jobs based on a cron expression, use RepositoryJobCronTrigger:

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

If you use the scheduler from a DaemonModule, ensure your module loads after the scheduler registration module by using the @RequiresService annotation.

Configuring Jobs in the Repository

You can configure jobs directly in the repository instead of registering them programmatically. Repository jobs are stored at /hippo:configuration/hippo:modules/scheduler/hippo:moduleconfig/. Under a job group of your choice, add a job node with the appropriate trigger:

/hippo:configuration/hippo:modules/scheduler/hippo:moduleconfig: /example: jcr:primaryType: hipposched:repositoryjob hipposched:attributeNames: [key] hipposched:attributeValues: [value] hipposched:repositoryJobClass: org.example.ExampleRepositoryJob /hipposched:triggers: jcr:primaryType: hipposched:triggers /example: jcr:primaryType: hipposched:crontrigger hipposched:cronExpression: 0 0/5 * * * ?

You can parameterize the RepositoryJob using the hipposched:attributeNames and hipposched:attributeValues properties. The key/value pairs are available in the RepositoryJobExecutionContext. The example above schedules a job to run every 5 minutes. You can add multiple triggers as needed.

Disabling Jobs and Triggers

Job and trigger nodes include the hipposched:enabled property. Set this property to disable the job or trigger. If the property is not set, the job or trigger is enabled by default.

Disabling the Scheduler on Specific Nodes in a Cluster

By default, all nodes in a clustered environment execute scheduled tasks. In some scenarios, such as delivery-only nodes, running scheduled jobs is not recommended because it may interfere with (de)publication tasks from CMS nodes.

To disable the scheduler on a node, add the following directive to the application container's startup script:

-Dhippo.scheduler.disabled=true
Share Feedback
Page: /build/spring-services/repository-scheduler
Section: Build
Category *