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

 

Leave a Reply

avatar

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

  Subscribe  
Notify of