Create an Abortable Job in SAP Hybris
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 usingclearAbortRequestedIfNeeded().
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 :
![]()
Solution Architect with 10+ years of experience across full-stack development, DevOps, and enterprise software design. Expert in building scalable architectures, CI/CD pipelines, and internal developer platforms using tools like GitHub Actions, Terraform, Kubernetes (AKS), Azure, and Prometheus. Strong hands-on background in Java, Spring, Node.js, and event-driven systems (Kafka, Mulesoft). Passionate about developer experience, software quality, and platform engineering
Hey! Nice tutorial!!
Can we use same piece of code for composite cronJob.?
by adding entry.getExecutableCronJob().setRequestAbort(true);
what if we do not override isAboortable and use clearAbortRequestIfNeeded(cronjobModel). Will it work?
Same content we can get it from SAP Help.I would request you to write one full working example.