Spring Batch:如何检索跳过的异常以进行测试

时间:2017-06-15 18:28:00

标签: spring-batch

我正在使用Spring Batch 3.0.7.RELEASE 版本

关于跳过我有以下内容:

<batch:chunk reader="personErrorSkipFileItemReader"
             processor="personErrorSkipItemProcessor"
             writer="personErrorSkipFileItemWriter"
             commit-interval="100" 
             skip-limit="11">
        <batch:skippable-exception-classes>
            <batch:include class="org.springframework.batch.item.file.FlatFileParseException"/>                                           
        <batch:include class="com.manuel.jordan.batch.exception.PersonBatchException"/>  
        </batch:skippable-exception-classes>         
</batch:chunk>

预期效果如何。

对于记录测试,我需要检索以下数据:

  • 跳过显示金额的元素(例如11个中的8个):
  • FlatFileParseException 5
  • PersonBatchException 3

更好的是,如果可以显示在哪个步骤和区域(读者,处理器,编写者)被抛出每个跳过的项目。

通过JobExecution,我有以下

List<Throwable> exceptions = jobExecution.getAllFailureExceptions();
logger.info("exceptions size: {}", exceptions.size());
for(Throwable throwable : exceptions){
    logger.error("Throwable Class: {}", throwable.getClass().getSimpleName());
    logger.error("{}", throwable.getMessage());
}
List<Throwable> exceptions_ = jobExecution.getFailureExceptions();
logger.info("exceptions_ size: {}", exceptions_.size());
for(Throwable throwable : exceptions_){
    logger.error("Throwable Class: {}", throwable.getClass().getSimpleName());
    logger.error("{}", throwable.getMessage());
}

但两个集合的大小都高于1,并在作业完成FAILED时显示例外情况。

因为8&lt; 11,Job完成COMPLETED的方式,列表大小返回0。

1 个答案:

答案 0 :(得分:1)

实现SkipListener,它有三个回调方法

1:onSkipInProcess 2:onSkipInWrite 3:onSkipInRead

http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/SkipListener.html