How to add exceptions to the step execution context? #5461
Replies: 1 comment 1 reply
|
It adds the So after you reload the If you need the information later, store a serializable summary yourself, for example: catch (Exception e) {
if (stepExecution != null) {
ExecutionContext context = stepExecution.getExecutionContext();
context.putString("processor.failure.type", e.getClass().getName());
context.putString("processor.failure.message", String.valueOf(e.getMessage()));
}
throw e;
}If the value is meant to explain the final failed step, another common option is to put a concise message in the exit status/exit description from a listener instead of storing the whole exception object: @Override
public ExitStatus afterStep(StepExecution stepExecution) {
// build a short, bounded description from your own collected failure summary
return stepExecution.getExitStatus().addExitDescription("...");
}I would avoid putting the raw So the short answer is: If this explains the repository behavior, please mark it as the answer so other Spring Batch users do not chase |

StepExecution.addFailureException(e)is not a way to persist an exception into the step execution context.It adds the
Throwableto the in-memoryStepExecution.failureExceptionslist. That list is useful while the step/job is running, but it is not one of the fields stored by the JDBC step execution DAO. The persisted step row contains things like status, counts, exit code, and exit message. The execution context is persisted separately as serializable key/value data.So after you reload the
StepExecutionfrom the repository, seeing an emptyfailureExceptionslist is expected.If you need the information later, store a serializable summary yourself, for example: