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 :
Software Craftsmanship, Stackextend author and Full Stack developer with 6+ years of experience in Java/Kotlin, Java EE, Angular and Hybris…
I’m Passionate about Microservice architectures, Hexagonal architecture, Event Driven architecture, Event Sourcing and Domain Driven design (DDD)…
Huge fan of Clean Code school, SOLID, GRASP principles, Design Patterns, TDD and BDD.
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.