首页 > 解决方案 > 保留有关失败项目处理的详细信息

问题描述

我有一个运行 TaskletStep 的作业,然后是一个基于块的步骤,然后是另一个 TaskletStep。在每个步骤中,都可能发生错误(以异常的形式)。

基于块的步骤如下所示:

stepBuilderFactory
            .get("step2")
            .chunk<SomeItem, SomeItem>(1)
            .reader(flatFileItemReader)
            .processor(itemProcessor)
            .writer {}
            .faultTolerant()
            .skipPolicy { _ , _ -> true } // skip all Exceptions and continue
            .taskExecutor(taskExecutor)
            .throttleLimit(taskExecutor.corePoolSize)
            .build()

整个工作定义:

jobBuilderFactory.get("job1")
            .validator(validator())
            .preventRestart()
            .start(taskletStep1)
            .next(step2)
            .next(taskletStep2)
            .build()

我预计 Spring Batch 会以某种方式获取沿途发生的异常,因此我可以在作业完成处理后创建一个包含它们的报告。查看不同的上下文,还有一些字段应该包含failureExceptions. 但是,似乎没有这样的信息(尤其是对于分块步骤)。

如果我需要以下信息,什么是一个好方法:

标签: kotlinspring-batch

解决方案


The JobExecution provides a method to get all failure exceptions that happened during the job. You can use that in a JobExecutionListener#afterJob(JobExecution jobExecution) to generate your report.

In regards to which items caused the issue, this will depend on where the exception happens (during the read, process or write operation). For this requirement, you can use one of the ItemReadListener, ItemProcessListener or ItemWriteListener to keep record of the those items (For example, by adding them to the job execution context to be able to get access to them in the JobExecutionListener#afterJob method for your report).


推荐阅读