How to Kill A Job Once it's Triggered in the Queue? | Community
Skip to main content
Level 3
March 29, 2024
Solved

How to Kill A Job Once it's Triggered in the Queue?

  • March 29, 2024
  • 2 replies
  • 951 views

I have a job in AEM that implements JobConsumer interface. My questions are:

 

1. Once it's triggered but not running yet, how to kill the job? 

2. Once it's running, how to kill the job?

 

 

I saw JobConsumer interface has `cancel` method but I am not sure how to use it. I found an example online and it's not using cancel() in the exception clause:

 

    @Overridepublic JobResult process(Job job) {
        try {
            logger.debug("Processing the JOB *******");

            //A Property map will be passed on so we can fetch the values we need here to//Process the request

            String path = (String) job.getProperty("path");
            logger.debug("The path in which the replication is triggered and passed to the Job is " +
                    "{}", path);

            //TODO : Write your business logic here . Any properties you need to execute the job can be passed//TODO: on via the  Map which is treated as the properties for the JOB.


            /**
             * Return the proper JobResult based on the work done...
             *
             * > OK : Processed successfully
             * > FAILED: Processed unsuccessfully and reschedule --> This will keep the JOB up for next retry
             * > CANCEL: Processed unsuccessfully and do NOT reschedule
             * > ASYNC: Process through the JobConsumer.AsyncHandler interface
             */return JobConsumer.JobResult.OK;
        } catch (Exception e) {
            logger.error("Exception is ", e);
            return JobResult.FAILED;
        }
    }
}

 

 

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 @aemuser2345,

To kill the job in both cases you have described, you could use removeJobById method from JobManager. Here is a simple example:

 

package com.mysite.core.jobs; import org.apache.sling.event.jobs.Job; import org.apache.sling.event.jobs.JobManager; import org.apache.sling.event.jobs.consumer.JobConsumer; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @Component(service=JobConsumer.class, property= { JobConsumer.PROPERTY_TOPICS + "=my/special/jobtopic" }) public class CustomJobConsumer implements JobConsumer { @Reference private JobManager jobManager; @Override public JobResult process(Job job) { if (jobManager.removeJobById(job.getId())) { return JobResult.CANCEL; } return JobResult.OK; } }

 

2 replies

lukasz-m
Community Advisor
lukasz-mCommunity AdvisorAccepted solution
Community Advisor
March 29, 2024

Hi @aemuser2345,

To kill the job in both cases you have described, you could use removeJobById method from JobManager. Here is a simple example:

 

package com.mysite.core.jobs; import org.apache.sling.event.jobs.Job; import org.apache.sling.event.jobs.JobManager; import org.apache.sling.event.jobs.consumer.JobConsumer; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @Component(service=JobConsumer.class, property= { JobConsumer.PROPERTY_TOPICS + "=my/special/jobtopic" }) public class CustomJobConsumer implements JobConsumer { @Reference private JobManager jobManager; @Override public JobResult process(Job job) { if (jobManager.removeJobById(job.getId())) { return JobResult.CANCEL; } return JobResult.OK; } }

 

aanchal-sikka
Community Advisor
Community Advisor
March 31, 2024

@aemuser2345 

 

Requesting you to please confirm if the suggested answers helped.

If yes, requesting you to please mark the correct answer. 

Aanchal Sikka