Skip to main content
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
lukasz-m
lukasz-mAccepted 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/

 

2 replies

krishna_sai
Community Advisor
Community Advisor
June 1, 2023

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

Translate
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/

 

Translate