目录
在对Spring Batch深入学习前,先写一个小的demo,增加对Spring Batch的了解
相关信息
开发环境:
JDK 1.8.0_201
apache-maven-3.3.9
IntelliJ IDEA 2019.2.1 (Community Edition)
版本信息
Spring Batch 3.0.10.RELEASE
Spring test 4.3.21.RELEASE
相关概念
领域对象 | 描述 |
---|---|
Job Repository | 作业仓库,负责Job,Step执行过程中的状态保存 |
Job launcher | 作业调度器,提供执行Job的入口 |
Job | 作业,由多个Step组成,封装整个批处理操作 |
Step | 作业步,Job的一个执行环节,由多个或者一个Step组装成Job |
Tasklet | Step中具体执行逻辑的操作,可以重复执行,可以设置具体的同步、异步操作等 |
Chunk | 给定数量Item的集合,可以定义对Chunk的读操作、处理操作、写操作,提交间隔等,这是Spring Batch框架的一个重要特征 |
Item | 一条数据记录 |
ItemReader | 从数据源(文件系统、数据库、队列等)中读取Item |
ItemProcessor | 在Item写入数据源之前,对数据进行处理(如:数据清洗、数据转换、数据过滤、数据校验等) |
ItemWriter | 将Item批量写入数据源(文件系统、数据库、队列等) |
demo背景
通过Spring Batch来读取csv文件,读完后再将读到的数据写入另一个csv文件
代码展示
项目结构
配置pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.et</groupId>
<artifactId>spring-batch-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-batch-demo</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>3.0.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.21.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version