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.
Solved! Go to Solution.
Views
Replies
Total Likes
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/
Hi @KKeerthi ,
you can refer to below blogs related to sling jobs
https://www.linkedin.com/pulse/aem-how-write-sling-jobs-aem-veena-vikraman/
https://redquark.org/aem/day-18-sling-jobs/
Hope this helps,
Krishna
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/