Spring Batch Hello World Example
Spring Batch is a framework for batch processing – execution of a series of jobs. In Spring Batch, A job consists of many steps and each step consists of a READ-PROCESS-WRITE
task or single operation
task (tasklet).
- For “READ-PROCESS-WRITE” process, it means “read” data from the resources (csv, xml or database), “process” it and “write” it to other resources (csv, xml and database). For example, a step may read data from a CSV file, process it and write it into the database. Spring Batch provides many made Classes to read/write CSV, XML and database.
- For “single” operation task (tasklet), it means doing single task only, like clean up the resources after or before a step is started or completed.
- And the steps can be chained together to run as a job.
1 Job = Many Steps.
1 Step = 1 READ-PROCESS-WRITE or 1 Tasklet.
Job = {Step 1 -> Step 2 -> Step 3} (Chained together)
Spring Batch Examples
Consider following batch jobs :
- Step 1 – Read CSV files from folder A, process, write it to folder B. “READ-PROCESS-WRITE”
- Step 2 – Read CSV files from folder B, process, write it to the database. “READ-PROCESS-WRITE”
- Step 3 – Delete the CSB files from folder B. “Tasklet”
- Step 4 – Read data from a database, process and generate statistic report in XML format, write it to folder C. “READ-PROCESS-WRITE”
- Step 5 – Read the report and send it to manager email. “Tasklet”
In Spring Batch, we can declare like the following :
<job id="abcJob" xmlns="http://www.springframework.org/schema/batch">
<step id="step1" next="step2">
<tasklet>
<chunk reader="cvsItemReader" writer="cvsItemWriter"
processor="itemProcesser" commit-interval="1" />
</tasklet>
</step>
<step id="step2" next="step3">
<tasklet>
<chunk reader="cvsItemReader" writer="databaseItemWriter"
processor="itemProcesser" commit-interval="1" />
</tasklet>
</step>
<step id="step3" next="step4">
<tasklet ref="fileDeletingTasklet" />
</step>
<step id="step4" next="step5">
<tasklet>
<chunk reader="databaseItemReader" writer="xmlItemWriter"
processor="itemProcesser" commit-interval="1" />
</tasklet>
</step>
<step id="step5">
<tasklet ref="sendingEmailTasklet" />
</step>
</job>
The entire jobs and steps execution are stored in database, which make the failed step is able to restart at where it was failed, no need start over the entire job.
1. Tutorial
In this Spring Batch tutorial, we will show you how to create a job, read a CSV file, process it, write the output to an XML file.
Tools and libraries used
- Maven 3
- Eclipse 4.2
- JDK 1.6
- Spring Core 3.2.2.RELEASE
- Spring OXM 3.2.2.RELEASE
- Spring JDBC 3.2.2.RELEASE
- Spring Batch 2.2.0.RELEASE
2. Project Directory
Review final project directory, a standard Maven project.
3. Project Dependencies
They must have dependencies are just Spring Core, Spring Batch and JDK 1.5. Read comments for self-explanatory.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mkyong</groupId>
<artifactId>SpringBatchExample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringBatchExample</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.6</jdk.version>
<spring.version>3.2.2.RELEASE</spring.version>
<spring.batch.version>2.2.0.RELEASE</spring.batch.version>
<mysql.driver.version>5.1.25</mysql.driver.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring jdbc, for database -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring XML to/back object -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId