同一个 Spring 批处理步骤中的多个 ItemProcessor 和 ItemWriters

Multiple ItemProcessors and ItemWriters in the same Spring Batch step

我可以用单个 ItemReader and multiple substeps, each with an ItemProcessor followed by a ItemWriter 编写一个 Spring 批处理步骤吗?

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

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

补充说明

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

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

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

默认情况下,Spring Batch 提供每个接口的两个实现:SimpleChunkProvider/FaultTolerantChunkProviderSimpleChunkProcessor/FaultTolerantChunkProcessor (Spring Batch 为远程 partitioning/chunking 提供了其他实现,但这超出了此处的范围)。每个组件默认使用“简单”版本。如果默认行为不够或无法根据您的需要进行配置,您可以提供自定义组件。请查看常见问题解答中的这个问题: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 的自定义实现,根据需要在管道中调用多个 processors/writers。使用这种方法,您将只读取一次项目,因为您可以继续使用默认的 ChunkProvider.