Schedule sling jobs | Community
Skip to main content
Level 2
June 1, 2023
Solved

Schedule sling jobs

  • June 1, 2023
  • 2 replies
  • 2591 views

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.

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 lukasz-m

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/

 

2 replies

krishna_sai
Community Advisor
Community Advisor
June 1, 2023
lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
June 4, 2023

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/