同一 Spring Batch 步骤中的多个 ItemProcessors 和 ItemWriters

问题描述

我可以编写一个带有单个 ItemReader 和多个子步骤的 Spring Batch 步骤吗?每个子步骤都有一个 ItemProcessor 后跟一个 ItemWriter

我正在努力实现这样的目标:

ItemReader ---> item ---> ItemProcessor#1 ---> ItemProcessor#2
                                 |                    |
                                 v                    v
                            ItemWriter#1         ItemWriter#2

附加说明

  • 为避免不一致,我不想读两遍。
  • 第二个 ItemProcessor 需要过滤掉一些项目,这些项目应该由第一个 ItemWriter 写入,而不是由第二个写入。

我认为这个问题与 Spring Batch: One reader,multiple processors and writers 不同,因为我需要按顺序处理项目,而不是并行处理。

解决方法

Spring Batch 提供的面向块的处理模型基于两个关键概念:

  • ChunkProvider:通过委托给 ItemReader
  • 来提供数据块
  • ChunkProcessor:通过委托给 ItemProcessorItemWriter
  • 来处理数据块

默认情况下,Spring Batch 为每个接口提供了两种实现:SimpleChunkProvider/FaultTolerantChunkProviderSimpleChunkProcessor/FaultTolerantChunkProcessor(Spring Batch 提供了远程分区/分块的其他实现,但这超出了这里的范围)。默认情况下,每个组件都使用“简单”版本。如果默认行为不够或无法根据您的需要进行配置,您可以提供您的自定义组件。请查看常见问题解答中的这个问题:What is the Spring Batch philosophy on the use of flexible strategies and default implementations? 以下是摘录:

We expect clients to create their own more specific strategies that can be
plugged in to control things like commit intervals (CompletionPolicy),rules about how to deal with exceptions (ExceptionHandler),and many others.

因此,为了回答您的问题,您可以提供 ChunkProcessor 的自定义实现,根据需要调用管道中的多个处理器/编写器。使用这种方法,您将只读取一次项目,因为您可以继续使用默认的 ChunkProvider

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...