Expand my Community achievements bar.

SOLVED

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

Avatar

Level 3

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;
        }
    }
}

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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;
    }
}

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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;
    }
}

 

Avatar

Community Advisor

@aemUser2345 

 

Requesting you to please confirm if the suggested answers helped.

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


Aanchal Sikka