Add delay of 5mins before running the sling job. | Community
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.
Best answer by lukasz-m

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.

3 replies

arunpatidar
Community Advisor
Community Advisor
December 6, 2024

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
lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
December 6, 2024

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.

Adobe Employee
December 6, 2024

Thanks @lukasz-m , will try this

narendiran_ravi
Level 6
December 7, 2024

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?