可跳过的例外

时间:2014-03-31 19:56:06

标签: spring-batch

我试图在批处理运行期间使用以下配置跳过所有异常:

<chunk reader="aaaFileReader" writer="aaaDBWriter"
                commit-interval="100" skip-limit="100000">
                <skippable-exception-classes>
                    <include class="java.lang.Exception" />
                    <exclude
                        class="org.springframework.jdbc.CannotGetJdbcConnectionException" />
                </skippable-exception-classes>

            </chunk>
            <listeners>
                <listener ref="aaabatchFailureListener" />
            </listeners>

我在听众中处理异常。但是当Spring Batch实际遇到异常时,它不被跳过,批处理运行以失败状态结束。实际的异常是FlatFileParse异常。如何跳过FlatFileParseException? 这是日志:

:18:21.257 [main] DEBUG o.s.b.repeat.support.RepeatTemplate - Handling fatal exception explicitly (rethrowing first of 1): org.springframework.batch.core.step.skip.NonSkippableReadException: Non-skippable exception during read
15:18:21.257 [main] ERROR o.s.batch.core.step.AbstractStep - Encountered an error executing the step
org.springframework.batch.core.step.skip.NonSkippableReadException: Non-skippable exception during read
    at org.springframework.batch.core.step.item.FaultTolerantChunkProvider.read(FaultTolerantChunkProvider.java:81) ~[spring-batch-core.jar:na]
    at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:106) ~[spring-batch-core.jar:na]
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:367) ~[spring-batch-infrastructure.jar:na]
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infr
Caused by: org.springframework.batch.item.file.FlatFileParseException: Parsing error at line: 5, input=[0254285458908060150983101150983         AK00055002035201401081044000804CK5861           00Twist,Oliver              AT&T                          20121208                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ]
    at org.springframework.batch.

3 个答案:

答案 0 :(得分:2)

您可以在批处理作业配置中添加 FlatFileParseException 类,例如:

<batch:chunk reader="customImportReader" writer="customImporter" processor="customProcessor" commit-interval="1" skip-limit="10">
    <batch:skippable-exception-classes>
        <batch:include class="org.springframework.batch.item.file.FlatFileParseException" />
    </batch:skippable-exception-classes>
</batch:chunk>

答案 1 :(得分:0)

  

根据 Spring批处理文档,一些例外不符合skippable的条件。

在你的情况下,从日志中明确org.springframework.batch.item.file.FlatFileParseException不是可跳过的异常,因此重新抛出org.springframework.batch.core.step.skip.NonSkippableReadException


详细了解Configuring Skip Logic部分内容:

  

对于遇到的任何异常,可跳过性将由类层次结构中最近的超类确定。任何未分类的异常都将被视为“致命”。

详细了解NonSkippableReadException说:

  

无法跳过读取操作时抛出致命异常。

答案 2 :(得分:0)

  1. 创建自定义fileReader并覆盖doRead()方法以始终抛出CustomException。

    public class CustomFlatFileItemReader扩展FlatFileItemReader { @覆盖 protected T doRead()抛出异常{     T itemRead = null;     尝试{         itemRead = super.doRead();     } catch(FlatFileParseException e){         抛出新的MyException(e.getMessage(),e);     }     return itemRead;} }

  2. 覆盖您的作业跳过政策,以便始终跳过自定义例外,如下所示:

    .skipPolicy((Throwable T,int skipCount) - &gt; {                 if(T instanceof BatchServiceException)                     返回true;                 其他                     return false;