Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Anchor
top
top
In some cases, a plugin should perform a periodical background operation. Examples are import or synchronization plugins that automatically import/update content in XperienCentral from an external source. This task should be performed on fixed, configurable, dates and times. Especially CPU consuming tasks should be scheduled to run at night. This section describes how to write such a scheduled task.

In This Section

Table of Contents
maxLevel2

...

The Service

A scheduled service is just a service component which invokes the schedule API. A service component can be built from the service component archetype - an example of such an implementation is described in /wiki/spaces/PD/pages/24721201. The basic service component implementation looks like this:

...

Note

The CustomJobService is the interface that identifies this service and is registered as the interface classname in the Activator. The run method is the method that should be invoked on each moment according to the time schedule. The onStart and onStop methods are invoked for starting/stopping the plugin and are thus the most suitable locations to schedule/unscheduled the task.

 

Back to Top

 

...

Schedule a Task

To schedule tasks, use the SchedulerService. This service provides the methods necessary for retrieving scheduled jobs and adding/removing new jobs to the schedule. The SchedulerService will invoke your service according to the schedule you provide.

...

Using the code example above, the run method will be invoked every 15 minutes. The name of the job is subject to the guidelines since only one job can use a certain name. Guideline /wiki/spaces/PD/pages/24721296 defines that this name must be prefixed by the plugin ID.

 

Back to Top

 

...

Session Management

If you implement a schedule task, one of the first issues you will face is session management. If, for example, you try to create a media item using the MediaRepositoryManagementService, the service will throw a "no authorization" exception. See /wiki/spaces/PD/pages/24721223 for more information.

 

Back to Top

 

...

Schedule Updates

The Crontab schedule used to schedule the job must be configurable. To make the Crontab expression configurable, the Configuration Management should be used. The scheduleJob method in the code example above should therefore retrieve the Crontab expression from the Configuration Management service and use that as input argument. If the Crontab expression is managed using the Configuration Management service, the schedule can be changed. However, in the code example above, a change in the schedule will not affect the jobs that are already scheduled unless the plugin is restarted manually. For that reason it is a good idea to reschedule the job if it is changed. For that purpose the Configuration Management service supports adding and removing listeners:

...