标签:
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).
1 Job = Many Steps.
1 Step = 1 READ-PROCESS-WRITE or 1 Tasklet.
Job = {Step 1 -> Step 2 -> Step 3} (Chained together)
Consider following batch jobs :
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.
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
Review final project directory, a standard Maven project.
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>mysql-connector-java</artifactId