距离开始使用 Spring Batch 有一段时间了,一直没有时间整理,现在项目即将完结,整理下这段时间学习和使用经历。

官网地址:http://projects.spring.io/spring-batch/

一、定义与特点

       A lightweight, comprehensive batch framework designed to enable the development of robust batch applications vital for the daily operations of enterprise systems.

Spring Batch provides reusable functions that are essential in processing large volumes of records, including logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management. It also provides more advanced technical services and features that will enable extremely high-volume and high performance batch jobs through optimization and partitioning techniques. Simple as well as complex, high-volume batch jobs can leverage the framework in a highly scalable manner to process significant volumes of information.

Features

  • Transaction management
  • Chunk based processing
  • Declarative I/O
  • Start/Stop/Restart
  • Rety/Skip
  • Web based administration interface (Spring Batch Admin)

二、简介

    Spring Batch 是一个依托 Spring,面向批处理的框架,可以应用于企业级数据处理系统。通过阅读官网文档,可以知道 Spring Batch 的核心组件包括 Job、Step 等。Spring Batch 不仅提供了统一的读写接口、丰富的任务处理方式、灵活的事务管理及并发处理,同时还支持日志、监控、任务重启与跳过等特性,大大简化了批处理应用开发,将开发人员从复杂的任务配置管理过程中解放出来,使他们可以更多地去关注核心的业务处理过程。

使用场景

  • Commit batch process periodically
  • Concurrent batch processing: parallel processing of a job
  • Staged, enterprise message-driven processing
  • Massively parallel batch processing
  • Manual or scheduled restart after failure
  • Sequential processing of dependent steps (with extensions to workflow-driven batches)
  • Partial processing: skip records (e.g. on rollback)
  • Whole-batch transaction: for cases with a small batch size or existing stored procedures/scripts

 

三、HelloWorld

程序简介:从指定路径的文本文件中逐行读取,获取用户的姓和名,并在处理器中拼接用户的姓名,最后输出用户的姓名。

操作系统:Win7 x64 旗舰版

开发环境:Eclipse 4.3 、JDK1.6

步骤:

1. 搭建开发工程

打开Eclipse, 新建 Java Project ,本例使用 SpringBatchTest 为项目名。

新建 lib 文件夹,导入 SpringBatch 的 Jar 包和其他依赖包。建立相关 package 和 class ,得到结构如下图:

[Spring Batch 系列] 第一节 初识 Spring Batch

其中 定义:

acc 存放访问控制类(本例准备存放作业测试类)

batch.listener 存放批处理监听器

batch.processor 存放 ItemProcessor实现类

batch.reader 存放 ItemReader 实现类

batch.writer 存放 ItemWriter 实现类

batch.mapper 存放逻辑对象映射处理类(本例准备存放文本行于文本行对象映射处理类)

batch.data 存放批处理过程中使用的逻辑对象

BatchServer.java 定义批处理任务方法接口

配置文件 定义:

spring-application-batch.xml  定义spring batch 核心组件和自定义作业

spring-application-resource.xml  定义spring 组件

spring-application-context.xml  配置文件引入使用的配置文件,并控制配置文件引入顺序

2. 编写配置文件和对应的程序代码

由于开发过程中,配置文件和程序是并行书写的,所以以下内容无特定顺序

(1) Spring Batch 配置文件及其中定义的组件实例

spring-application-batch.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:batch="http://www.springframework.org/schema/batch"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans  
 6             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
 7             http://www.springframework.org/schema/batch   
 8             http://www.springframework.org/schema/batch/spring-batch-2.2.xsd"
 9     default-autowire="byName">
10 
11     <!-- Spring Batch  内存模型 -->
12     <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />
13     <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
14         <property name="jobRepository" ref="jobRepository" />
15     </bean>
16     <bean id="taskExecutor" class="org.springframework.core.task.SyncTaskExecutor" />
17 
18 
19     <!-- 文本行与逻辑对象映射处理 -->
20     <bean id="customerLineMapper" class="cn.spads.batch.mapper.CustomLineMapper"/>
21     <bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" >
22         <property name="delimiter" value=" "/>
23     </bean>
24 
25     <!-- Scope = step 变量后绑定固定写法,即可以在对象调用时绑定变量 -->
26     <bean id="customReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
27         <property name="lineMapper">
28             <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
29                 <property name="lineTokenizer" ref="lineTokenizer"/>
30                 <property name="fieldSetMapper" ref="customerLineMapper"/>
31             </bean>       
32         </property>
33         <!-- 此处使用单一文件绝对路径 -->
34         <property name="resource" value="file:#{jobParameters['customFileAbPath']}"/>
35     </bean>
36 
37     <bean id="customProcessor" class="cn.spads.batch.processor.CustomProcessor"/>
38     <bean id="customWriter" class="cn.spads.batch.writer.CustomWriter"/>
39 
40     <bean id="customJobListener" class="cn.spads.batch.listener.CustomJobListener"/>
41     <bean id="customStepListener" class="cn.spads.batch.listener.CustomStepListener"/>
42 
43 
44     <batch:job id="customJob">
45         <batch:step id="customJob_first_step">
46             <batch:tasklet>
47                 <batch:chunk reader="customReader" processor="customProcessor" 
48                     writer="customWriter" commit-interval="100">
49                 </batch:chunk>
50                 <batch:listeners>
51                     <batch:listener ref="customStepListener" />
52                 </batch:listeners>
53             </batch:tasklet>
54         </batch:step>
55         <batch:listeners>
56             <batch:listener ref="customJobListener"/>
57         </batch:listeners>
58     </batch:job>
59 </beans>

由于本示例使用Spring Batch 提供 的固定长度文本加载实例(FlatFileItemReader),因此没有自定义 Reader。

LineVo.java

package cn.spads.batch.data;

/**
 * <b>文本行逻辑对象</b><br>
 * @author        Gaylen
 * @version        V1.1.0
 * history
 * 1.1.0, 2014年11月24日        Gaylen            FE
 * @since        Java 6.0
 */
public class LineVo {

    /** 行号 */
    private int id;
    /***/
    private String givenName;
    /***/
    private String familyName;
    /** 全名 */
    private String fullName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGivenName() {
        return givenName;
    }

    public void setGivenName(String givenName) {
        this.givenName = givenName;
    }

    public String getFamilyName() {
        return familyName;
    }

    public void setFamilyName(String familyName) {
        this.familyName = familyName;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
}
View Code

相关文章:

猜你喜欢
相关资源
相似解决方案