I have a somewhat linear job set up in Spring Batch that consists of several steps. If, at any point, a single step fails, the job should fail.
The steps consist of a number of tasklets followed by a chunk-based step. I.e.:
- Step 1
- Tasklet 1
- Step 2
- Tasklet 2
- Step 3
- Reader
- Processor
- Writer
If something goes wrong, the obvious thing to do is throw an Exception. Spring Batch will handle this and log everything. This behaviour, particularly printing the stack trace, is undesirable, and it would be better if the Job could be ended gracefully with the Status set to FAILED
.
The Tasklets currently set the ExitStatus
directly on the StepContribution
. They are also built using a flow (which wasn't ideal, but the steps continue unhindered otherwise). The issues can then be handled directly in the Tasklet.
However, we have no access to the StepContribution
in the chunk-based approach. We only have the StepExecution
. Using setExitStatus
does nothing here.
We are using the builders (JobBuilerFactory
and StepBuilderFactory
), not the XML setups.
The potential solutions:
- Tell or configure Batch how to handle exceptions (not to print a stack trace).
- Catch the exception in a listener. Unfortunately, the exception has already been caught by Spring Batch by the time it gets to the
@AfterStep
. - Tell the step/job that we do not want to continue (e.g. setting a value in the execution context or an alternative for the
StepContribution
.