springspring-batchitemprocessoritemwriter

Spring Batch: One reader, multiple processors and writers


In Spring batch I need to pass the items read by an ItemReader to two different processors and writer. What I'm trying to achieve is that...

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

This is needed because items written by ItemWriter#1 should be processed in a completely different way compared to the ones written by ItemWriter#2. Moreover, ItemReader reads item from a database, and the queries it executes are so computational expensive that executing the same query twice should be discarded.

Any hint about how to achieve such set up ? Or, at least, a logically equivalent set up ?


Solution

  • This solution is valid if your item should be processed by processor #1 and processor #2

    You have to create a processor #0 with this signature:

    class Processor0<Item, CompositeResultBean>
    

    where CompositeResultBean is a bean defined as

    class CompositeResultBean {
      Processor1ResultBean result1;
      Processor2ResultBean result2;
    }
    

    In your Processor #0 just delegate work to processors #1 and #2 and put result in CompositeResultBean

    CompositeResultBean Processor0.process(Item item) {
      final CompositeResultBean r = new CompositeResultBean();
      r.setResult1(processor1.process(item));
      r.setResult2(processor2.process(item));
      return r;
    }
    

    Your own writer is a CompositeItemWriter that delegate to writer CompositeResultBean.result1 or CompositeResultBean.result2 (look at PropertyExtractingDelegatingItemWriter, maybe can help)