How to setup schedulers in AEM?

jezwn

02-12-2019

I've created a scheduler class and wrote the configuration and run() method for scheduler. I need a System.out at intervals based on the cron-expression. How can I set the scheduler interval to the cron-expression and make it run?

Reading through the Scheduler API instead of whiteboard almost all the methods was found deprecated. What's the alternate for that?

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.Reference;

import org.osgi.service.metatype.annotations.AttributeDefinition;

import org.osgi.service.metatype.annotations.Designate;

import org.osgi.service.metatype.annotations.ObjectClassDefinition;

@Designate(ocd=ScheduledNodeProperty.Config.class)

@Component(service = Runnable.class)

public class ScheduledNodeProperty implements Runnable {

@ObjectClassDefinition(name="Node Create/Update Scheduler",

description = "Test")

public static @interface Config {

@AttributeDefinition(name = "Cron Expression for Execution Interval")

String execution_interval() default "0 * * * * ?";

}

private String interval;

@Reference

private Scheduler scheduler;

@Activate

protected void activate(final Config config) {

this.interval = config.execution_interval();

}

public void run() {

System.out.println("Executing a cron job (job#1) through the whiteboard pattern");

}

}

View Entire Topic

WASIL

Employee

03-12-2019

Try below :


@Component(property={"scheduler.expression:String=0 * * * * ?"})

@Designate(ocd=ScheduledNodeProperty.Config.class) 

public class ScheduledNodeProperty implements Runnable

Thanks

Wasil