Efficiently Managing Tasks in AEM: Leveraging the Power of Sling Jobs and Scheduled Jobs
Sling Jobs and Sling Scheduled Jobs are two powerful tools that allow developers to perform tasks asynchronously in Adobe Experience Manager (AEM). These tools provide a way to execute tasks in the background, without blocking the user’s experience, and are essential for building efficient and performant AEM applications. In this article, we will explore the differences between Sling Jobs and Sling Scheduled Jobs, and provide examples of how to use each one in your AEM projects.
In Adobe Experience Manager (AEM), both Sling Jobs and Sling Scheduled Jobs are used to perform background tasks, but they have some key differences:
- Execution time: Sling Jobs are executed immediately once they are added to the queue, whereas Sling Scheduled Jobs are executed at a specific time or on a regular schedule.
- Scheduling: Sling Jobs do not have built-in scheduling functionality and need to be triggered manually. Sling Scheduled Jobs have built-in scheduling functionality and are executed automatically at the specified time or schedule.
- Persistence: Sling Jobs are not persistent, which means that if the system is restarted, the job is lost. Sling Scheduled Jobs are persistent, which means that they will be executed at the next scheduled time even if the system is restarted.
- Priority: Sling Jobs can be assigned a priority level, which determines the order in which they are executed. Sling Scheduled Jobs do not have priority levels.
- API: Sling Jobs can be added, removed and queried with the Sling Job API. Sling Scheduled Jobs can be scheduled, updated, and cancelled with the Job Manager API, in addition to getting the status of the jobs and getting the list of all scheduled jobs.
Sling Jobs:
To write a Sling Job in Adobe Experience Manager (AEM) 6.5 and AEM as a Cloud Service (AEMaaCS), you need to implement the org.apache.sling.event.jobs.Job interface and register it with the Job Manager. Here are the general steps to create a Sling Job:
- Create a new Java class and implement the org.apache.sling.event.jobs.Job interface.
public class MySlingJob implements Job {
public JobResult process(Job job) {
// Job logic here
return JobResult.OK;
}
}2. Annotate the class with the @Component and @Service annotations, and set the properties for the job.
@Component
@Service(value = Job.class)
@Properties({
@Property(name = Job.PROPERTY_JOB_TOPIC, value = "my/job/topic"),
@Property(name = Job.PROPERTY_JOB_NAME, value = "My Sling Job"),
@Property(name = Job.PROPERTY_JOB_DESCRIPTION, value = "This is my custom Sling Job")
})
public class MySlingJob implements Job {
public JobResult process(Job job) {
// Job logic here
return JobResult.OK;
}
}3. Implement the logic for the job in the process() method. The same is mentioned in step 2 as well.
public JobResult process(Job job) {
// Job logic here
return JobResult.OK;
}4. Build and deploy the bundle to AEM.
5. Once the bundle is deployed, you can use the Job Manager console or the Job Manager API to add the job to the queue.
JobManager jobManager = sling.getService(JobManager.class);
Map<String, Object> props = new HashMap<>();
props.put("param1", "value1");
props.put("param2", "value2");
jobManager.addJob("my/job/topic", props);6. You can also use the Job Manager API to get the status of the job, remove the job from the queue, or query the list of all jobs.
Note that the above is just a simplified example, in practice the job’s processing will be more complex and you should handle exceptions and take care of the job’s result.
In AEMaaCS, you need to make sure that your job runs within the limitations and restrictions of the platform. This includes, for example, the time limit for each job, the number of concurrent jobs that can run, and the available memory and CPU resources.
Sling Scheduled Jobs:
Here’s an example of how to create a Sling Scheduled Job in AEM 6.5 and AEMaaCS:
- Create a new Java class that implements the
Runnableinterface. This class will contain the logic of your scheduled job.
@Component(service = Runnable.class, property = {
SchedulerConstants.PROPERTY_SCHEDULER_EXPRESSION + "=0 0 1 * * ?",
SchedulerConstants.PROPERTY_SCHEDULER_CONCURRENT + "=false"})
public class MyScheduledJob implements Runnable {
@Override
public void run() {
// Perform task here
}
}2. Annotate the class with @Component and set the service property to Runnable. Also set the property property to include the schedule expression and whether the job should be executed concurrently or not. In this example, the job is set to run at 1 AM every day using the Quartz cron expression "0 0 1 * * ?" and it is set to not run concurrently.
3. In AEMaaCS, you’ll need to create an OSGi configuration that defines the schedule for your job. In the Configuration Manager, create a new configuration for the com.adobe.granite.scheduler.Job component, and set the scheduler.expression property to the desired schedule.
4. Build and deploy your bundle to AEM.
5. Verify that the job is running as expected by checking the logs, or by using the Scheduled Jobs dashboard in the AEM web interface.
Writing a Sling Scheduled Job in AEM 6.5 and AEM as a Cloud Service (AEMaaCS) is relatively similar to the process in previous versions of AEM. The main difference is that in AEMaaCS, you need to take into account the cloud-specific features and limitations.
Please note that in AEMaaCS, only the jobs that are active in the current week are executed. If you want to schedule a job that runs next week or in the future, you will need to activate it manually. Also, keep in mind that the maximum interval that you can set for a scheduled job is one day.
It’s also worth noting that the above example is a basic one, for a production-ready job you may want to handle errors, logging, and so on.
In conclusion, Sling Jobs and Sling Scheduled Jobs are powerful tools for performing tasks asynchronously in AEM. They allow developers to execute tasks in the background, without blocking the user’s experience, and are essential for building efficient and performant AEM applications. Understanding the differences between Sling Jobs and Sling Scheduled Jobs, and how to use them effectively, is crucial for optimizing AEM performance and providing a positive user experience. With the examples and best practices provided in this article, you should now be well-equipped to implement Sling Jobs and Sling Scheduled Jobs in your AEM projects and take your AEM development skills to the next level.
Would you like to support me? You can support my work if you enjoy reading my stories. Sign up for my newsletter and get my stories straight to your inbox.
To get access to my unlimited stories, you can also consider signing up to become a Medium member for just $ 5. If you sign up using my link, I will receive a small commission (at no extra cost to you).





