What is the correct way to handle exceptions in Spring Batch listeners?

When using an ItemWriteListener (or the like) from Spring Batch, what is the proper way to handle checked exceptions that were passed to listener delegates?

For instance:

 public class MyClass implements ItemWriteListener<MyDTO> { // fields and constructor omitted @Override public void beforeWrite(List<MyDTO> items) { try { repository.loadAll(); // <-- Throws some checked exception } catch (Exception e) { // What to do here? } } // other methods omitted } 

How should I handle this exception? Here are some ideas:

  • Repeat in RuntimeException
  • Record the exception and let the step fail along the line where it depends on the action of this listener
  • Set some indicator in MyDTO elements to indicate failure

Of course, I cannot throw a checked exception because it is not declared in the ItemWriteListener interface.

+4
source share
1 answer

Throw an exception at runtime if it is up to the reader / writer. This way, you can be sure that SpringBatch correctly handles transaction and updates to SpringBatch tables.

+5
source

All Articles