2
votes

I am new to Spring Batch and I have to design a task which reads from database and write the data in to multiple XMLs the output format is as follows

    <Records xmlns"somevalue" ...>
  <Version>1.0</Version>
  <SequenceNo>1</SeqeunceNo>
  <Date>12/12/2012 12:12:12 PM<Date>
  <RecordCount>100</RecordCount><!--This is total number of Update and Insert txns-->
  <SenderEmail>[email protected]</SenderEmail>
  <Transaction type="Update">
    <TxnNo>1</TxnNo>
    <Details>
      <MoreDetails>
      </MoreDetails>
    </Details>
  </Transaction>
  <Transaction type="Insert">
    <TxnNo>2</TxnNo>
    <Details>
      <MoreDetails>
      </MoreDetails>
    </Details>
  </Transaction>
  <Transaction type="Update">
  </Transaction>
  <Transaction type="Update">
  </Transaction>
</Records>

Please suggest what unmarshaller should I use and how to start on this. Eventually later I have to convert it to multithreading for optimization and performance.

2

2 Answers

0
votes

No need to write your own writer. Spring include a MultiResourceItemWriter to write your items into multiple xml. I'm using a jaxb2Marshaller to write my complex XML.

<bean id="multiItemWriter" class="org.springframework.batch.item.file.MultiResourceItemWriter"> 
    <property name="resource" value="file:data/output/output.xml"/> 
    <!-- <property name="resourceSuffixCreator" ref="resourceSuffixCreator"/> --> 
    <property name="saveState" value="true"/> 
    <property name="itemCountLimitPerResource" value="10"/> 
    <property name="delegate" ref="itemWriter" />
</bean>

<bean id="itemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
    <!-- <property name="resource" value="file:data/output/output.xml" /> -->
    <property name="marshaller" ref="customVrdbMarshaller" />
    <property name="rootTagName" value="recordings" />
    <property name="overwriteOutput" value="true" />
</bean>

<bean id="customVrdbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
    <property name="classesToBeBound">
        <list>
            <value>your.model.model.Albums</value>
        </list>
    </property>
</bean>
0
votes

You should code a Writer that writes XML files. Choose a lib and use it in a Writer.

Be careful to write thread safe code for your future multithreading optimization.

An example from Spring Batch samples : XML Processing