AEM Schedulers


          Apache Sling Scheduler enables you to easily schedule jobs within your application. Jobs can be executed at a specific time, regularly at a given period or at the time given by a cron expression by leveraging the Sling scheduler service.

Scheduler in AEM
For Creating a scheduler in AEM, follow below steps -
- Create an OSGi configuration to read the scheduler specific values from the user i.e. cron expression, the name of the scheduler, custom parameter etc.
- Create a sling scheduler which displays the custom parameter at an interval specified by the cron expression.

import org.apache.sling.commons.scheduler.ScheduleOptions;
import org.apache.sling.commons.scheduler.Scheduler;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.metatype.annotations.Designate;
import org.demo.core.services.CustomSchedulerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author Sailaxman
 *
 * A Sling Scheduler demo using OSGi R6 annotations
 *
 */
@Component(immediate = true, service = CustomScheduler.class)
@Designate(ocd = CustomSchedulerConfig.class)
public class CustomScheduler implements Runnable {

private static final Logger log = LoggerFactory.getLogger(CustomScheduler.class);
private String customParam;

/**
 * Id of the scheduler based on its name
 */
private int schedulerId;

/**
 * Scheduler instance injected
 */
@Reference
private Scheduler scheduler;

/**
 * Activate Method to initialize stuff
 */
@Activate
protected void activate(CustomSchedulerConfig config) {
 // Getting the scheduler id
schedulerId = config.schdulerName().hashCode();

 // Getting the custom parameter
customParam = config.customParam();
}

/**
 * Method Modifies the scheduler id on modification
 */
@Modified
protected void modified(CustomSchedulerConfig config) {
//Removing the scheduler
removeScheduler();

// Updating scheduler id
schedulerId = config.schdulerName().hashCode();

//Again adding the scheduler
addScheduler(config);
}

/**
 * Method deactivates the scheduler and removes it
 */
@Deactivate
protected void deactivate(CustomSchedulerConfig config) {
//Removing the scheduler
removeScheduler();
}
/**
 * This method removes the scheduler
 */
private void removeScheduler() {
log.info("Removing scheduler: {}", schedulerId);
//Unscheduling/removing the scheduler
scheduler.unschedule(String.valueOf(schedulerId));
}
/**
 * Method adds the scheduler
 */
private void addScheduler(CustomSchedulerConfig config) {
// Check if scheduler is enabled or not
if(config.enabled()) {
// Scheduler option takes the cron expression as a parameter and run accordingly
ScheduleOptions scheduleOptions = scheduler.EXPR(config.cronExpression());
// Adding params
scheduleOptions.name(config.schdulerName());
scheduleOptions.canRunConcurrently(false);

// Scheduling job
scheduler.schedule(this, scheduleOptions);
log.info("Scheduler added");
} else {
log.info("Scheduler is Disabled");
}
}
/**
 * Overridden run method to execute
 */
@Override
public void run() {
log.info("Custom Scheduler is now running using the passed custom paratmeter, customParam {}", customParam);
}
}
  • First, we are registering the class as a service and implementing the Runnable interface. At the same time, using @Desginate annotation, we are linking the OSGi configuration created in the previous section with this class.
  • Now, we are injecting the org.apache.sling.commons.scheduler.Scheduler dependency.
  • In the activate() method, we are reading the required values. Then we are getting the schedulerId from the scheduler name.
  • The modified() method recalculates the schedulerId in case the OSGi configuration is modified.
  • In the addScheduler() method, we are registering the scheduler using the Scheduler API.
  • The run() method will be defining our task. Here we are just printing the customParameter in the logs.