Skip to content

Latest commit

 

History

History
30 lines (21 loc) · 1.25 KB

File metadata and controls

30 lines (21 loc) · 1.25 KB

##11.1 日志项处理和失败

一个常见的用例是需要在一个步骤中特殊处理错误,chunk-oriented步骤(从创建工厂bean的这个步骤)允许用户实现一个简单的ItemReadListener用例,用来监听读入错误,和一个ItemWriteListener,用来监听写出错误.下面的代码片段说明一个监听器监听失败日志的读写: >public class ItemFailureLoggerListener extends ItemListenerSupport {

private static Log logger = LogFactory.getLog("item.error");

public void onReadError(Exception ex) {
    logger.error("Encountered error on read", e);
}

public void onWriteError(Exception ex, Object item) {
    logger.error("Encountered error on write", ex);
}

}

在实现此监听器必须注册步骤:

<step id="simpleStep">
 ...
 <listeners>
   	<listener>
     	   &lt;bean class="org.example...ItemFailureLoggerListener"/>
    	</listener>
 </listeners>
</step>

记住,如果你的监听器在任何一个onError()方法中,它将在一个事务中回滚.如果在一个onError()方法中需要使用事务性的资源(比如数据库),可以考虑添加一个声明式事务方法(有关详细信息,请参阅Spring核心参考指南),并给予其传播属性REQUIRES_NEW值。