Anchor | ||||
---|---|---|---|---|
|
Info |
---|
See Scheduling Jobs for information on the Cron format that XperienCentral uses to schedule regularly performed tasks. |
In This Topic
Table of Contents | ||
---|---|---|
|
...
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 Configuration Management. The basic service component implementation looks like this:
...
public class CustomJobServiceImpl extends SimpleServiceComponent implements CustomJobService {
public void onStart() {
// Run the job only on the master and not on the read-only slaves
if (myJcrRepositoryService.isReadOnly()) {
LOG.warning("This cluster is read only. This job is only executed on the master. Job '" + JOB_NAME + "' not scheduled.");
return;
}
public void onStop() {
// Stop listening to update events on the schedule
myConfigurationManagement.removeListener(this);
// Remove the job from the schedule
unscheduleJob();
}
public void run(SchedulerJob job) {
// Check whether this instance is the cluster task master
if (!myFrameworkConfig.isClusterTaskMaster()) {
LOG.log(Level.INFO, "Execution of job '" + JOB_NAME + "' skipped on this server.");
return;
}
} |
Note |
---|
The |
...
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.
...
The job is usually scheduled in the onStart()
method and removed in the onStop()
method. To cancel the job, the removeSchedulerJob(String jobName)
method with the name of the job can be invoked. The code snippet below shows an example of how to schedule the job. The schedule itself is defined by a Crontab expression. See also the XperienCentral JavaDoc for the SchedulerJob
for the exact syntax that you need to use for the expression.
...
...
The
...
run
...
Note |
---|
Exception handling is omitted from this code example. |
Using the code example above, the run
method will be invoked every 15 minutes. Concurrent runs is are disabled, so if the run
method takes longer than 15 minutes, the next job will not be run. The name of the job is subject to the guidelines since only one job can use a certain name. Guideline G151 defines that this name must be prefixed by the plugin ID.
...
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 Session Management for more information.
...
Schedule Updates
The Crontab schedule used to schedule the job must be configurable. To make the Crontab expression the Crontab expression configurable, the Configuration Management should be used. The scheduleJob
method in the code example above should therefore retrieve the Crontab expression the Crontab expression from the Configuration Management service and use that as input argument. If the Crontab expression 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:
...
theme | Eclipse |
---|
...
.
...
The implementation of the ConfigurationManagementListener
can register itself on changes in the configuration set that contain the schedule and reschedule the job when there is a change.
...
Concurrent Runs
By default, jobs run concurrently. The run()
run
method may be subsequently invoked by another thread before the the previous run has completed. You can disable concurrent runs on the SchedulerJob
instance using job.setAllowConcurrentRuns(
false
)
.