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

 

5 1 vote
Article Rating
Subscribe
Notify of
guest

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

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Saagar bhambhani
Saagar bhambhani
4 years ago

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

Naved
Naved
3 years ago

what if we do not override isAboortable and use clearAbortRequestIfNeeded(cronjobModel). Will it work?

Albert
Albert
1 year ago

Same content we can get it from SAP Help.I would request you to write one full working example.

3
0
Would love your thoughts, please comment.x
()
x