1

I would like to get some insight on the possibility of implementing a spring batch CompositeWriter equivalent in Java EE 7 jsr 352 batch.

The JSL won't allow me to include multiple ItemWriters in a step. My approach is to introduce a delegate pattern in single ItemWriter to execute multiple sql statements just like the spring batch CompositeWriter. Please see sample code below.

<job id="myJob" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd" version="1.0">
    <step id="step1">
        <chunk item-count="1000">
            <reader ref="JdbcCursorItemReader"/>
            <writer ref="myCompositeItemWriter"/>
        </chunk>    
    </step>
</job>


Pseudo code for myCompositeItemWriter:

MyCompositeItemWriter implements ItemWriter {
     List<ItemWriter> delegates;

     public void open() {
      //initialize delegates here
     }

     public void write(List<Object> chunkedItems) {
        for (ItemWriter myWriter: delegates) {
             myWriter.write(chunkedItems);
          }
       }

}

gkc123
  • 512
  • 1
  • 7
  • 24

1 Answers1

1

From the JSR-352 specification, section "8.2.1.3 Writer":

A chunk type step must have one and only one item writer.

Nothing in the spec talks about composing writers. So you need to create a custom composite writer implementing the delegate pattern as you described and use it as an item writer in your step. Obviously, you would have this for free if you use Spring Batch as your JSR-352 implementation.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Thanks for confirming Mahmoud. I am able to get this working in my junit test. I noticed that the spring-batch version implements ItemStreamWriter. Please recommend if there are any other item that need to be considerd while overriding ItemWriter open and close methods for the delegates. – gkc123 Jun 17 '20 at 17:40