Create an Abortable Job in SAP Hybris

Hybris Logo

1. Overview

Sometimes we face a situation where we need to stop/abort a running cronjob, whether because is taking a lot of time or it was executed with the wrong parameters.

Unfortunately, cronjobs are not abortable by default, in this article, I will show you how to create an abortable job in SAP Hybris.

Note that this article will not cover the details of creating a cronjob, find details about creating a cronjob in this article.

2. Implementation

You can make a cronjob abortable by adding two things in your JobPerfomable class :

  • Override isAbortable() method.
@Override
public boolean isAbortable() {
	return true;
}
  • In the perform() method, regularly check for abortion requests using clearAbortRequestedIfNeeded().
if(clearAbortRequestedIfNeeded(cronJobModel)) {
	log.info("Job aborted manually !");
	return new PerformResult(CronJobResult.ERROR, CronJobStatus.ABORTED);
}

By applying these two steps, your JobPerformable will look as follow.

public class AbortableJob extends AbstractJobPerformable<AbortableCronJobModel> {

    final static Logger log = Logger.getLogger(AbortableJob.class);

    @Override
    public PerformResult perform(HelloWorldCronJobModel cronJobModel) {
        
        for (int iteration = 0; iteration<10000; iteration++) {

            log.info("iteration : " + iteration);

            if(clearAbortRequestedIfNeeded(cronJobModel)) {
                
                log.info("Job aborted manually !");
                return new PerformResult(CronJobResult.ERROR, CronJobStatus.ABORTED);
                
            }
        }
        return new PerformResult(CronJobResult.SUCCESS, CronJobStatus.FINISHED);
    }

    @Override
    public boolean isAbortable() {
        return true;
    }
}

To Send an abortion request to the Running Cronjob :

  • Using CronJobService :
@Autowired
private CronJobService cronJobService;

cronJobService.requestAbortCronJob(CronJobModel);
  • From HMC :

Abort Cronjob from HMC in SAP Hybris

 

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
3 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Saagar bhambhani Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
Saagar bhambhani
Guest
Saagar bhambhani

Hey! Nice tutorial!!
Can we use same piece of code for composite cronJob.?
by adding entry.getExecutableCronJob().setRequestAbort(true);