Expand my Community achievements bar.

SOLVED

Add delay of 5mins before running the sling job.

Avatar

Employee

Hi,

We have written a listener to run a sling job whenever the asset is modified. Is there a way to add a delay of 5mins to jobManager.addJob() then run the sling job?

Thank you.


Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Keerthana_H_N

You can use Scheduled Jobs, to achieve your goal. Below is a sample code snippet:

 

import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.JobBuilder.ScheduleBuilder;
import java.util.Calendar;
import java.util.Date;

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
// increasing time by 5 minutes
calendar.add(Calendar.MINUTE, 5);
Date date = calendar.getTime();
        
ScheduleBuilder scheduleBuilder = jobManager.createJob("your topic").schedule();
scheduleBuilder.at(date);
if (scheduleBuilder.add() == null) {
  // something went wrong here, use scheduleBuilder.add(List<String>) instead to get further information about the error
}

 

In above snippet following things are done:

  1. Prepare date doing simple math: current date + 5 minutes.
  2. Preparing a scheduled job that will be fired at time defined in step 1.

Using at(Date date) method job will be run once at given time.

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @Keerthana_H_N 

Maybe you can create a workflow laucher thats wait for 5 min and then executed or queue the job.



Arun Patidar

Avatar

Correct answer by
Community Advisor

Hi @Keerthana_H_N

You can use Scheduled Jobs, to achieve your goal. Below is a sample code snippet:

 

import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.event.jobs.JobBuilder.ScheduleBuilder;
import java.util.Calendar;
import java.util.Date;

Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
// increasing time by 5 minutes
calendar.add(Calendar.MINUTE, 5);
Date date = calendar.getTime();
        
ScheduleBuilder scheduleBuilder = jobManager.createJob("your topic").schedule();
scheduleBuilder.at(date);
if (scheduleBuilder.add() == null) {
  // something went wrong here, use scheduleBuilder.add(List<String>) instead to get further information about the error
}

 

In above snippet following things are done:

  1. Prepare date doing simple math: current date + 5 minutes.
  2. Preparing a scheduled job that will be fired at time defined in step 1.

Using at(Date date) method job will be run once at given time.

Avatar

Level 6

Hi @Keerthana_H_N ,

Are you creating sling jobs when an asset is modified?

I assume the Sling job is executed using a Job Consumer, which likely calls a service to perform the desired action.

To manage execution, you can define the queue configuration using the Apache Sling Job Queue Configuration. Set the property: queue.type as ORDERED and queue.maxparallel: as 1 to prevent parallel execution of multiple jobs. This configuration ensures jobs are executed sequentially, avoiding potential conflicts or race conditions.

Could you elaborate on the specific issues you’re facing due to parallel execution of Sling jobs?