Schedulers

Last updated on 30th September, 2017 by George Taylor

While a scheduler is just a service that implements the Runnable interface it is a very useful service in AEM.

Scheduling in AEM is done with the quartz library, while you don't have to understand quartz to write a scheduler it is good background knowledge.

There are a few ways you can tell your scheduler to run.

Scheduling with a cron expression

The property for the service that defines a cron expression is scheduler.expression

@Component
@Service(value = Runnable.class)
@Property( name = "scheduler.expression", value = "0 * * * * ?")

For more information on the quartz cron expressions go to Quartz Cron Documentation.

As the service is of type Runnable it has to implement the Runnable interface and have a run method which is where the service can process

Scheduling at peridoic times

The property for the service that defines a periodic run is scheduler.period

@Component
@Service(value = Runnable.class)
@Property( name = "scheduler.period", longValue = 10)

This example will run the processes every 10 seconds

Manually Scheduling a task

To manually schedule a task you have to have access to the scheduler service.

The following are examples of scheduling via a cron expression, a period or a specific time

String schedulingExpression = "0 15 10 ? * MON-FRI";
this.scheduler.addJob("myJob", job, null, schedulingExpression, true);
long period = 3 * 60; //the period is expressed in seconds
this.scheduler.addPeriodicJob("myJob", job, null, period, true);
SimpleDateFormat formatter = newSimpleDateFormat("yyyy/MM/dd");
String date = "2020/01/10";
java.util.Date fireDate = formatter.parse(date);
this.scheduler.fireJobAt("myJob", job, null, fireDate);

Additional properties

The following properties are available to the scheduler, but may require additional configuration within AEM and the scheduler service

  • scheduler.concurrent
    • Determines if multiple scheduler threads can run the same process
  • scheduler.runOn
    • The server within the cluster that the process can run on
    • This will require configuration of the scheduler server to determine each of the clusters names