Sling Jobs in AEM: Streamlining Your Background Tasks for Maximum Efficiency

Techzette
3 min readMar 1, 2023

--

Sling Jobs in AEM

Sling Jobs is a powerful feature of Adobe Experience Manager (AEM) that allows developers to run background tasks asynchronously. It provides a reliable and efficient way to handle long-running and resource-intensive operations without affecting the performance of the AEM instance.

In this article, we will explore the basics of Sling Jobs in AEM and how to leverage it to build scalable and robust applications.

What is Sling Jobs and how it works?

Let us understand the Sling Jobs using a real-life story of a restaurant kitchen.

Imagine you are the head chef of a busy restaurant kitchen, and you have to prepare a large number of orders every day. You have a team of chefs and other cooks to help you, but some of the dishes take a lot of time and resources to prepare. For example, making a complex sauce or slow-cooking a tough piece of meat can take hours.

To ensure that all the orders are prepared efficiently and on time, You assign specific tasks to each member of your team and create a schedule for each task to be completed.

As the head chef, you can monitor the progress of each task, If a task is taking longer than expected, you can reassign it to another team member or adjust the schedule to ensure that all the orders are prepared on time.

In the above story you can compare the tasks assigned to chefs as Jobs in AEM.

So from above example we can define Sling Job as below.

Sling Jobs allow you to assign tasks to specific threads or instances, set priorities, and monitor their progress.

By using Sling Jobs, you can ensure that long-running and resource-intensive operations in your AEM application are handled efficiently and without affecting the performance of the overall system.

Use-cases of Sling Jobs in AEM

  1. Generating Reports
  2. Asynchronous Data Imports
  3. Email Notifications
  4. Content Archival
  5. Asset Management background Tasks
  6. Workflow Automation

Steps to create Sling Job in AEM

  1. Create a Job Consumer

The first step is to create a job consumer that defines the logic for processing the job. This can be done by implementing the org.apache.sling.event.jobs.consumer.JobConsumer interface.

package com.adobe.aem.guides.wknd.core.slingjobs;

import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.osgi.service.component.annotations.Component;

@Component(service = JobConsumer.class, immediate = true, property = {
JobConsumer.PROPERTY_TOPICS + "=" + "my/job/topic"
})
public class FirstJobConsumer implements JobConsumer{

@Override
public JobResult process(final Job job) {
// Logic for processing the job
// Read the properties that are injected from the Job Producer
String property = (String) job.getProperty("customParam");
return JobResult.OK;
}

}

In this example, we create a job consumer that listens for jobs on the “my/job/topic” topic. The process() method is where the logic for processing the job is defined.

2. Create a Job Producer:

we need to create a job producer that triggers the job. This can be done by injecting the org.apache.sling.event.jobs.JobManager interface and calling the addJob() method.

package com.adobe.aem.guides.wknd.core.services;

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

import java.util.HashMap;
import java.util.Map;

@Component
public class JobProducerService {

@Reference
private JobManager jobManager;

public void triggerJob() {
Map<String, Object> jobProperties = new HashMap<>();
jobProperties.put("customParam", "value");
jobManager.addJob("my/job/topic", jobProperties);
}

}

In this example, we inject the JobManager interface and call the addJob() method to trigger the job. We also set the “customParam” parameter to a new value of “value”.

You can use the trigger job method either from the Sling Servlet, Sling Scheduler, Sling Filters, Sling Model and Custom Workflow Process in AEM.

In AEM, Sling Jobs are stored in the JCR (Java Content Repository) repository under the /var/eventing/jobs node by default.

Please find the full code in the github link below.

https://github.com/techzette/aem/tree/develop

If you enjoyed and learned reading the article, please give it a thumbs up, share it, and subscribe for future updates.

~MV

To Learn more about AEM and code with help of video follow the youtube channel below.

https://www.youtube.com/channel/UCtZnDp9R3UkEEpUWah1DpkQ

One click away to discover more about the JavaScript, Angular, React, and other frontend frameworks.

https://www.youtube.com/c/Techshareskk

--

--

Techzette

Learn more about the web development, AEM, MarTech, architecture and how systems are designed.