How to setup schedulers in AEM?

Avatar

Avatar

jezwn

Avatar

jezwn

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");

}

}

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

PuzanovsP

MVP

Avatar

PuzanovsP

MVP

PuzanovsP
MVP

03-12-2019

Dear Jez,

Nothing better can explain the change as the creators notes:

Carsten Ziegeler added a comment - 26/Jul/13 07:59 

This is a builder based approach which allows us to add more options in the future without the need to add more methods to the scheduler
In addition, the methods now return a boolean rather than throwing an exception
And the number of possibilities with the new api is reduced as some of the old methods have some overlap which now could be reduced down to 5 ways.

Copied from : [SLING-2981] Create simpler but more flexible scheduler service api - ASF JIRA

In order to make API more verbose and less repetitive Sling Devs have create two common methods:

schedule and unschedule

However, to keep enterprise customers happy between upgrades they kept all the legacy API's and simply marked them as deprecated.

You should feel comfortable using both to schedule and unschedule for your tasks to print out your sysout's.

Hope this helps.

Regards,

Peter

Answers (1)

Answers (1)

Avatar

Avatar

WASIL

Employee

Avatar

WASIL

Employee

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