2

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>asds@asds.com</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.

Shriram
  • 103
  • 3
  • 7

2 Answers2

0

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

agilob
  • 6,082
  • 3
  • 33
  • 49
Jean-Philippe Briend
  • 3,455
  • 29
  • 41
  • Thanks Jean but I am not able to find out any example which is writing in to the complex xml. I did used XStreamMarshaller but I am not sure how can use it for the xml mentioned in the question. Also any tips to use the existing spring batch multi threading for this task – Shriram Jul 03 '12 at 16:50
  • Check this for multithreading : http://stackoverflow.com/questions/2326695/how-to-set-up-multi-threading-in-spring-batch/3282317#3282317 – Jean-Philippe Briend Jul 03 '12 at 16:53
0

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>
Grégory
  • 1,473
  • 1
  • 17
  • 28