当使用Spring Batch的 ItemWriteListener
(或类似)时,处理被检查异常的正确方法是什么?是侦听器的委托?
例如:
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
}
我该如何处理这个例外?以下是一些想法:
-
Rethrow in a
RuntimeException
-
记录异常并让步骤失败,直到它依赖于此侦听器的操作
-
在
MyDTO
项目上设置一些指示符以指示失败
当然,我不能抛出已检查的异常,因为它未在 ItemWriteListener
接口上声明 .
1 回答
如果读取器/写入器依赖于此操作,则抛出运行时异常 . 这样,您可以确保SpringBatch正确处理事务和SpringBatch db-tables的更新 .