Reputation: 835
We have a jsr 352 java batch application that we are wanting to stop the execution early if a condition is met but not throw an exception. The basic layout of our job is as follows:
<job id=XXX>
<step id="XXX">
<listeners>
<listener ref=XXXXX /> <--Step listener
<listener ref=XXXX /> <-- Item process listener
</listeners>
<chunk checkpoint-policy="item" item-count="1">
<reader ref=XXXXXX />
<processor ref=XXXXXX />
<writer ref=XXXXXX />
</chunk>
</step>
</job>
During our step listener, in the beforeStep() method, if a specific condition is met we do not want to continue the rest of the batch job, we just want to gracefully stop. Up until now the only things that would end the batch job would be an exception being raised for an error scenario, or a successful completion of the job.
How would we accomplish skipping the rest of the step, since its the only step, if that condition is met inside the step listener? Also, is throwing an exception the recommended way to end the batch process during an error scenario, or is there a better/more recommended way?
Upvotes: 1
Views: 284
Reputation: 1138
You can have an external component to watch for the condition and stop the current job execution upon the condition. Calling JobOperator stop method will stop the job execution gracefully.
Or you can have your item reader checks for the condition, and returns null upon true which will naturally end the step execution.
Upvotes: 1