How to setup schedulers in AEM? | Adobe Higher Education
Skip to main content
jezwn
Level 5
December 3, 2019
해결됨

How to setup schedulers in AEM?

  • December 3, 2019
  • 2 답변들
  • 6693 조회

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

}

}

이 주제는 답변이 닫혔습니다.
최고의 답변: Peter_Puzanovs

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

2 답변

Peter_Puzanovs
Community Advisor
Community Advisor
December 3, 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

Adobe Employee
December 3, 2019

Try below :


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

@Designate(ocd=ScheduledNodeProperty.Config.class) 

public class ScheduledNodeProperty implements Runnable

Thanks

Wasil

February 24, 2023

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