gkc123
gkc123

Reputation: 522

Spring batch CompositeWriter Equivalent in Java EE jsr352

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);
          }
       }

}

Upvotes: 1

Views: 112

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31600

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.

Upvotes: 1

Related Questions