Spring BatchSprings

Spring Batch Read an XML file and write to Oracle Database

In this post we will show you, how to use Spring Batch to read an XML file with your ItemReader using StaxEventItemReader and write its data to Oracle Database using Custom ItemWriter. We will also learn how to use ItemProcessor to process input data before writing to database.

Custom ItemReader or ItemWriter is a class where we write our own way of reading or writing data. In Custom Reader we are required to handle the chunking logic as well. This comes in handy if our reading logic is complex and cannot be handled using Default ItemReader provided by spring.

For introduction to Spring batch and to learn basics, click here.

Tools and libraries used

  1. Maven 3
  2. Eclipse 4.2
  3. JDK 1.8
  4. Spring Core 3.2.2.RELEASE
  5. Spring Batch 2.2.0.RELEASE
  6. Spring OXM 3.2.2.RELEASE

1. Java Maven Project

Create simple java maven project

File -> New -> Maven Project

2. Project Dependencies

Declares all project dependencies in the pom.xml

 

3. Input XML File

resources/sample-input-report.xml

 

 

4. Spring Batch Job configuration.

4.1. Read XML input

Read XML input file using default ItemReader provided by spring StaxEventItemReader. If our reading logic is straight forward and no complex logics involved then its better to use default ItemReader as it works well.

 

4.2 Batch Job configuration

Spring Job contains steps, where each steps does its work one after another based on SUCCESS or FAILURE of the step. Step contains tasklet which can again be divided into 2 categories.

  1. Tasklet
  2. Chunk

Tasklet are meant to perform single task in one step for the complete data. All the steps like read, process and write will be done in one step and once finished it will exit.

Chunk : In chunk based approach it performs action on chunk of data and not on the while as in Tasklet. It performs Read, Process and Write in one step only for chunk of the data defined in configuration. Then again it reads new chunk on data and repeats the process until it finishes the data.

Chunk based flow:

While there’re N lines:

  • Do for X amount of lines:
    • Read one line
    • Process one line
  • Write X amount of lines.

In above example if you notice, chunk of data is defined using commit-interval property.

Spring Job context XML

In this context xml we define our Spring Batch job and also its steps that will be executed to perform the required actions.

resources/job-context.xml

JAXB Marshaller is used to map XML root, element and its properties to Java object.

 

4.3 Spring Batch Core Settings

Core setting xml file context.xml contains all job launcher and job repository settings which holds the meta data information about the Spring batch job, also we have datasource configured here to connect our Oracle database.

resources/context.xml

5. Java Source Classes:

Contract.java

 

ItemProcessor is used to process the data coming from input source, If want to perform some data manipulation before writing it to DB like date formatting, text formatting etc. then in that case ItemProcessor is the right place.

CustomItemProcessor.java

 

ItemReader reads each record from input and sends it to ItemProcessor for further processing and finally when chunk size limit is  reached, it is sent to ItemWriter for writing to output.

ItemWriter.java

 

 

JDBCBaseDao.java

 

Listeners:

Listeners are like interceptor that help to intercept the execution of a Job or a Step and allow the user to perform some functionality. In listeners we have 2 methods beforeStep() and afterStep(). beforeStep() is called before the job starts step execution and afterStep() is called once the step has finished its job.

StepListener.java

 

 

SpringApp.java

 

Output:

 

Hope you like the tutorial, lets us know by comments. Happy Learning… !!!!

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.