Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Schedule sling jobs

Avatar

Level 3

How to schedule sling jobs  do we need to write a scheduler and call Sling Job from scheduler or there is any other way to do this.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @KKeerthi,

If you want to schedule sling job you should use ScheduleBuilder object created by schedule() method from JobBuilder. Using scheduled sling jobs you do not need create scheduler by yourself, it is done automatically in the background.

Here is very simple example, from Sling documentation:

import org.apache.sling.jobs.JobManager;
import org.apache.sling.event.jobs.JobBuilder.ScheduleBuilder;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(immediate=true)
public class MyComponent {

    private static final String TOPIC = "midnight/job/topic";

    @Reference
    private JobManager jobManager;

    public void startScheduledJob() {
        Collection<ScheduledJobInfo> myJobs = jobManager.getScheduledJobs(TOPIC, 1, null);
        if (myJobs.empty()) {
            // daily invocation not yet scheduled
            ScheduleBuilder scheduleBuilder = jobManager.createJob(TOPIC).schedule();
            scheduleBuilder.daily(0,0); // execute daily at midnight
            if (scheduleBuilder.add() == null) {
                // something went wrong here, use scheduleBuilder.add(List<String>) instead to get further information about the error
            }
        }
    }
}

You will find all the required information about sling schedule jobs under:

As a supplement to you can also have a look on a Sling Scheduler, here is a great article that describes the differences between Sling Scheduled Jobs and Sling Scheduler - https://cqdump.joerghoh.de/2022/06/20/sling-scheduled-jobs-vs-sling-scheduler/

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @KKeerthi,

If you want to schedule sling job you should use ScheduleBuilder object created by schedule() method from JobBuilder. Using scheduled sling jobs you do not need create scheduler by yourself, it is done automatically in the background.

Here is very simple example, from Sling documentation:

import org.apache.sling.jobs.JobManager;
import org.apache.sling.event.jobs.JobBuilder.ScheduleBuilder;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(immediate=true)
public class MyComponent {

    private static final String TOPIC = "midnight/job/topic";

    @Reference
    private JobManager jobManager;

    public void startScheduledJob() {
        Collection<ScheduledJobInfo> myJobs = jobManager.getScheduledJobs(TOPIC, 1, null);
        if (myJobs.empty()) {
            // daily invocation not yet scheduled
            ScheduleBuilder scheduleBuilder = jobManager.createJob(TOPIC).schedule();
            scheduleBuilder.daily(0,0); // execute daily at midnight
            if (scheduleBuilder.add() == null) {
                // something went wrong here, use scheduleBuilder.add(List<String>) instead to get further information about the error
            }
        }
    }
}

You will find all the required information about sling schedule jobs under:

As a supplement to you can also have a look on a Sling Scheduler, here is a great article that describes the differences between Sling Scheduled Jobs and Sling Scheduler - https://cqdump.joerghoh.de/2022/06/20/sling-scheduled-jobs-vs-sling-scheduler/