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
ItemProcessor
will need to filter out some items, which should be written by the first ItemWriter
, 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 the ItemReader
ChunkProcessor
: processes chunk of data by delegating to the ItemProcessor
and ItemWriter
By default, Spring Batch provides two implementations of each interface: SimpleChunkProvider
/FaultTolerantChunkProvider
and SimpleChunkProcessor
/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:
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.
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 default ChunkProvider
.