Sap Hybris Tip 4 – Abort Cronjob – Enable Abort Action
In the previous post Hybris Tutorial – Cronjob | Task Schedule | Export Order Data to CSV | Unit 8, we learned the way to create the cronjob. But we don’t know how to stop it when it is executing. So in this post, we will learn about the way to abort cronjob.
By default, when implementing the job, the abort action is disabled and we also need to check the proper places to stop cronjob to data integrity.
Implementation steps.
- Enable Abort Action
Basically, this action looks like a flag to tell the running cronjob that “I want to stop the current task immediately”. So the abort flag has 2 states: On/Off. By default, this flag is always off and does NOT allow the user to turn it on.
To enable this action, we should override the isAbortable method in the job definition:
@Override
public boolean isAbortable()
{
return true;
}
Alternatively, if you extend the AbstractJobPerformable, you may override a bean property value in Spring configuration
2. Checking the proper places to stop cronjob
Based on step 1, the abort action is enabled, when the user clicks on the abort action, the abort flag is on, but processes of job behind the scenes continue running. So in the job definition, we need to update the code to check the abort flag continuously to abort the cronjob at the right places.
Calling the clearAbortRequestedIfNeeded of the AbstractJobPerformable. It checks if the given cronJob is requested to be aborted and has REQUESTABORT flag set to true. If so, the flag is set to false again and the job execution is prematurely stopped.
if(clearAbortRequestedIfNeeded(cronJob))
{
//abort the job
//do some clean-up
return new PerformResult(CronJobResult.ERROR, CronJobStatus.ABORTED);
}
Thanks for reading.
References:
https://userapps.support.sap.com/sap/support/knowledge/en/2728389