Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

3 Replies

Avatar

Community Advisor

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

Avatar

Level 2

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

Avatar

Correct answer by
Community Advisor

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