Skip to content
Advertisement

Spring batch run exception throwable method after each tasklet done in afterstep()

JavaScript

This is my example code using tasklet in spring boot. My problem is: I want to throw exception from afterstep(), but the interface does not allow it.

Despite this limitation, why I obsessed with afterstep() is that I want to make abstract class to make Tasklet template which can verify each execution in afterstep(). I want verification to run after all execute() is done, which will be overridden by subclass. So I have no choices but using afterstep().

Any idea to run verification method after each execute() with throwable or afterstep() can pass Exception to logExceptionHandler()? I hope to define logExceptionHandler() in TaskletConfiguration class. It will be obese if it is defined in Tasklet class, as I will make abstract class, which will be inherited by many subclasses.

Advertisement

Answer

The StepExecutionListener#afterStep is not designed to throw checked exceptions. Here is an excerpt from its Javadoc:

JavaScript

Moreover, even if you throw a (runtime) exception in afterStep, the exception won’t be passed to the exception handler, it will only be logged as mentioned in the Javadoc.

I think it is too late to throw exceptions in StepExecutionListener#afterStep, this method can be used to check the status of the step execution and modify the ExitStatus if needed to drive the rest of the job execution flow.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement