Expand my Community achievements bar.

SOLVED

How to setup schedulers in AEM?

Avatar

Level 6

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

}

}

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

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

Avatar

Employee Advisor

Try below :


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

@Designate(ocd=ScheduledNodeProperty.Config.class) 

public class ScheduledNodeProperty implements Runnable

Thanks

Wasil

Avatar

Level 1

Nothing better can explain the change as the creators notes:

Imagine you are new and you see thart, it explains nothing...

You need some annoptation, see here:

https://stackoverflow.com/a/75552213/2332337