如何处理从项目读取器引发的异常? [英] How to handle the exceptions thrown from item reader?

查看:129
本文介绍了如何处理从项目读取器引发的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕获从项目读取器抛出的异常(例如读取器未打开,不正确的令牌异常等)并进行处理。当前,春季批处理将它们作为致命的异常抛出,并脱离了这一步。

I want to catch the exceptions thrown from item reader (e.g. reader not open , incorrect token exceptions etc) and handle it. Currently spring batch is throwing them as fatal exceptons and come out of the step.

请让我知道是否有办法?

Please let me know if there is any way to do it?

推荐答案

我遇到了同样的问题,即我想抓住
org.springframework.batch.item FlatFileItemReader 抛出的.file.FlatFileParseException
并执行一些自定义处理&记录。做了一些研究,几乎得出结论,我可能不得不编写一个自定义阅读器,而不是我当前使用的默认阅读器,直到我偶然发现了Spring Batch文档中某个部分的瑰宝: http://docs.spring.io/spring-batch/reference/html/configureStep.html#interceptingStepExecution

I faced the same issue whereby I wanted to catch the org.springframework.batch.item.file.FlatFileParseException thrown by the FlatFileItemReader and perform some custom handling & logging. Did some research and almost reached the conclusion that I might have to write a custom reader instead of the default reader I was currently using, until I stumbled upon a gem of a section in the Spring Batch documentation: http://docs.spring.io/spring-batch/reference/html/configureStep.html#interceptingStepExecution

您可以编写 ItemReadListener< T> 接口,并覆盖 onReadError(Exception ex) 方法,然后在相应的步骤中注册此侦听器类。这样,当读取器从文件读取时遇到异常时,将调用此方法。异常引用也将传递给该方法,您可以使用该方法随意进行日志记录等操作。
同样,编写 @OnReadError 如果您不想单独实现 ItemReadListener 接口,也可以使用带注释的方法。

You can write a custom implementation of the ItemReadListener<T> interface and over-ride the onReadError(Exception ex) method and then register this listener class in the corresponding step. As such, this method will then be called when the reader encounters an exception while reading from the file. The exception reference will be passed to the method as well using which you can do as you please like logging etc. Similarly, writing a @OnReadError annotated method is also an alternative if you don't want to implement the ItemReadListener interface separately.

换句话说,如果您的整个目的是跳过读取时可能发生的此类异常,则可以尝试将以下内容添加到XML的块配置中:

On a different note, if your whole purpose is to skip such exceptions that might occur while reading, you can try adding following to the chunk configuration in the XML:

<skippable-exception-classes>
     <include class="org.springframework.batch.item.file.FlatFileParseException"/>
</skippable-exception-classes>

参考: http://docs.spring.io/spring-batch/reference/html/configureStep.html#configuringSkip

问题解决了! :)

这篇关于如何处理从项目读取器引发的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆