Dynamic Scheduler on given date in aem as a cloud service. | Community
Skip to main content
Level 2
April 19, 2023
Solved

Dynamic Scheduler on given date in aem as a cloud service.

  • April 19, 2023
  • 1 reply
  • 1227 views

Hi Team,

 

I have a requirment. The requirment is, once the bundle got activated it has to get the date from page and create a scheduler with that date.

once scheduler gets triggered on given date and time it has to do some x task and again it has to get the date and time from page and schedule a new scheduler.

 

I have created a apache scheduler (org.apache.sling.commons.scheduler.Scheduler) and it's working in local but not in cloud (some times it worked but suddenly it stoped working with no errors in logs) and also i tried sling job (org.apache.sling.event.jobs.consumer.JobConsumer) it working in local but not in cloud. Could anyone help me to achive the requirement using scheduler or any other ways.

 

Thanks in advance.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ShaileshBassi

Hi @naresh536 Yes the piece of code shared above by you, does not work.
Try out the using jobManager.addJob(JOB_TOPIC, props);

Code Snippet

@Override public void run() { Map<String, Object> props = new HashMap<>(); jobManager.addJob(JOB_TOPIC, props); }

 This worked for us.

Hope this helps!

Thanks

1 reply

ShaileshBassi
Community Advisor
Community Advisor
April 19, 2023

Hi @naresh536 There are issue with scheduler on AEM Cloud instance.

It is recommended to use the Sling Jobs for writing the business logic to be executed at a particular time.

 

@Component(immediate=true, service=Runnable.class, property = {Constants.SERVICE_ID + "=Test Scheduler"} public class Test implements Runnable { @Reference private JobManager jobManager; @Reference private Scheduler scheduler; @Override public void run() { Map<String, Object> props = new HashMap<>(); jobManager.addJob(JOB_TOPIC, props); } @Activate @Modified protected void activate(final Config config) { scheduler.unschedule("scheduler-name"); final ScheduleOptions scheduleOptions = scheduler.EXPR(<Scheduler-CRON-EXP>); scheduleOptions.name("scheduler-name"); scheduleOptions.canRunConcurrently(false); scheduler.schedule(this, scheduleOptions); } }

 

 Now create the JobConsumer class and make sure the job topic is same in this and the above scheduler class.

Create a common static reference in JobConsumer and use in the Scheduler as shown in the sample code.

 

 

public static final String JOB_TOPIC = "job/test";

 

Hope this helps!
Thanks
Naresh536Author
Level 2
April 19, 2023

Thanks for the response @shaileshbassi .

 

I have implemented the same jobManager. it worked in local but not in  cloud. i have followed the below link

https://blog.developer.adobe.com/handling-sling-schedulers-in-aem-as-a-cloud-service-cb59d5e59e9

Below will be called from active method.


pblic void startScheduledJob() {
Collection<ScheduledJobInfo> myJobs = jobManager.getScheduledJobs();
if (myJobs.isEmpty()) {
Calendar cal = getPageTTL();
ScheduleBuilder scheduleBuilder = jobManager.createJob(TOPIC).schedule();
cal.add(Calendar.MINUTE, 1);
Date date = new Date(cal.getTimeInMillis());
scheduleBuilder.at(date);
if (scheduleBuilder.add() == null) {
LOGGER.info("is empty LOGGER");
// something went wrong here, use scheduleBuilder.add(List<String>) instead to get further information about the error
} else {
LOGGER.info("is non empty LOGGER::{}",date);
}
}

ShaileshBassi
Community Advisor
ShaileshBassiCommunity AdvisorAccepted solution
Community Advisor
April 20, 2023

Hi @naresh536 Yes the piece of code shared above by you, does not work.
Try out the using jobManager.addJob(JOB_TOPIC, props);

Code Snippet

@Override public void run() { Map<String, Object> props = new HashMap<>(); jobManager.addJob(JOB_TOPIC, props); }

 This worked for us.

Hope this helps!

Thanks