Spring Book – Chapter 21 – Spring Batch

XML

There are two commonly used XML parsers in market: DOM and SAX. DOM parser loads the entire file into memory which makes it not useful for batch processing due to performance implications. Spring Batch uses StAX parser, which is similar to the event-based SAX parser but has the advantage of allowing for the ability to parse sections of document independently making its performance much better compared to SAX.

How the parsing of XML works

The XML is first divided into fragments and then these fragments are supplied to the Spring OXM which does the necessary parsing of the XML file as shown in Figure 23-12 below.

Figure 23-12. How parsing of an XML works

Figure 23-12. How parsing of an XML works

Spring Batch uses Object/XML Mapping (OXM) to bind fragments to objects. Spring OXM is used because it provides uniform abstraction for the most popular OXM technologies.

StaxEventItemReader

Spring Batch provides org.springframework.batch.item.xml.StaxEventItemReader to parse XML input file. To use this ItemReader you define a fragment root element name, which identifies the root element of each fragment as shown in Figure 23-9 above. Another input parameter which needs to be supplied is the org.springframework.oxm.Unmarshaller implementation which will be used to convert the input XML to appropriate business object of your application. Spring provides unmarshaller implementations that use Castor, JAXB, JiBX, XMLBeans, and XStream in their oxm package as shown in Figure 23-9 above. Configuring a StaxEventItemReader along with appropriate marshaller is as shown in Listing 23-9 below.

Listing 23-9. Configuring StaxEventItemReader in the Spring configuration file

StaxEventItemWriter

StaxEventItemWriter, the Streaming API for XML (StAX) implementation allows Spring Batch to write fragments of XML as each chunk is processed. It generates the XML a chunk at a time and writes it to the file after the local transaction has been committed. By doing this it prevents rollback issues if there is an error while writing to the file.

The configuration of the StaxEventItemReader consists of the file to read from, a root element name of the XML, and an unmarshaller which will convert the XML input into an object as shown in Listing 23-10 below.

Listing 23-10. Configuring StaxEventItemWriter in the Spring configuration file

Page Visitors: 25939

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 21 – Spring Batch

  1. Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?

  2. Please provide an example for StoredProcedureItem Reader – which returns cursor and how to process cursor in processor – I am new to Spring Batch as part of my work I need to invoke Oracle Stored proc which takes one param as input and returns result set which I need to process in Spring Batch Processor.

    Thanks
    Laxmi

Leave a Reply

Your email address will not be published. Required fields are marked *