【问题标题】:Spring Batch: How retrieve the skipped exceptions for Testing purposesSpring Batch:如何检索跳过的异常以进行测试
【发布时间】:2017-11-18 08:37:00
【问题描述】:

我正在使用Spring Batch3.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>

它按预期工作。

出于 loggingtest 目的,我需要检索以下数据:

  • 跳过的元素(例如 11 个中的 8 个)显示数量,例如:
  • FlatFileParseException5
  • PersonBatchException3

如果可以显示每个跳过的项目在哪个步骤和区域(读取器、处理器、写入器)中被抛出,那就更好了。

通过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 并且仅在 Job 完成时显示异常FAILED

因为 8 Job 完成了 COMPLETED 和列表大小返回 0 的方式。

【问题讨论】:

    标签: spring-batch


    【解决方案1】:

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

    1 :onSkipInProcess 2:onSkipInWrite 3: onSkipInRead

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

    【讨论】:

    • 它仅适用于运行时目的,方法返回void。我需要为@Test 目的检索该数据,用于assertEquals 评估。
    • 您正在测试批处理的单个步骤/tasklet?
    • 我想要一个步骤和所有步骤。因此这两种情况。
    猜你喜欢
    • 2014-04-26
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-30
    • 1970-01-01
    • 2019-02-01
    相关资源
    最近更新 更多