Can I write a Spring Batch step with a single ItemReader and multiple substeps, each with an ItemProcessor followed by a ItemWriter?
I am trying to achieve something like this:
ItemReader ---> item ---> ItemProcessor#1 ---> ItemProcessor#2
| |
v v
ItemWriter#1 ItemWriter#2
Additional notes
- To avoid inconsistencies, I would prefer not to read items twice.
- The second
ItemProcessor
will need to filter out some items, which should be written by the firstItemWriter
, but not by the second one.
I believe this question to be a different question from Spring Batch: One reader, multiple processors and writers because I would need to process items sequentially, and not in parallel.
The chunk-oriented processing model provided by Spring Batch is based on two key concepts:
ChunkProvider
: provides chunks of data by delegating to theItemReader
ChunkProcessor
: processes chunk of data by delegating to theItemProcessor
andItemWriter
By default, Spring Batch provides two implementations of each interface:
SimpleChunkProvider
/FaultTolerantChunkProvider
andSimpleChunkProcessor
/FaultTolerantChunkProcessor
(Spring Batch provides other implementations for remote partitioning/chunking but this is out of scope here). The "simple" version is used by default for each component. If the default behaviour is not enough or can't be configured according to you needs, you can provide your custom components. Please take a look at this question from the FAQs: What is the Spring Batch philosophy on the use of flexible strategies and default implementations? Here is an excerpt:So to answer your question, you can provide a custom implementation of
ChunkProcessor
that calls multiple processors/writers in pipeline as you want. With this approach, you will read items only once since you can keep using the defaultChunkProvider
.