Spring Batch 批处理框架 埃森哲和Spring Source研发

主要解决批处理数据的问题,包含并行处理,事务处理机制等。具有健壮性 可扩展,和自带的监控功能,并且支持断点和重发。让程序员更加注重于业务实现。

Spring Batch 结构如下

JobRepository :作业仓库 负责job。step执行过程的状态保存

JobLauncher  :  作业调度器 提供执行job入口

Job : 作业 由一个或者多个step组成 封装多个批处理操作,每个step可以有自己的上下文存放变量和自己的生命周期。

Step: 作业步 job的一个环节,有多个或者一个step组成job

Tasklet: Step中的具体执行逻辑的动作 可循环执行,支持异步和同步 适用于不同场景

Chunk :给定的item集合 可以定义对chunk的读操作,处理操作,写操作,提交间隔等 这是SpringBatch 一个特性

Item:一条数据记录

ItemReader: 从数据源(文件系统 队列 文件等)读取item

ItemProcessor:在Item写入前 进行一些处理 比如数据清洗,数据转换,数据校验,数据过滤等。

ItemWrieter :将item批量输出到数据源(文件系统,队列,数据库等)

看下batch的基本配置元素 分为两种 一种为在内存中。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
default-autowire="byName">
<!--job工厂 不是定时-->
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
</bean>
  <!--job加载器-->
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository"/> </bean>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/> </beans>

第二种为放在数据库中 需要在数据库中建表 脚本在spring-batch-core的core包里面。mysql版本如下 下一篇文章解释表的作用

-- Autogenerated: do not edit this file

CREATE TABLE BATCH_JOB_INSTANCE  (
JOB_INSTANCE_ID BIGINT NOT NULL PRIMARY KEY ,
VERSION BIGINT ,
JOB_NAME VARCHAR() NOT NULL,
JOB_KEY VARCHAR() NOT NULL,
constraint JOB_INST_UN unique (JOB_NAME, JOB_KEY)
) ENGINE=InnoDB; CREATE TABLE BATCH_JOB_EXECUTION (
JOB_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY ,
VERSION BIGINT ,
JOB_INSTANCE_ID BIGINT NOT NULL,
CREATE_TIME DATETIME NOT NULL,
START_TIME DATETIME DEFAULT NULL ,
END_TIME DATETIME DEFAULT NULL ,
STATUS VARCHAR() ,
EXIT_CODE VARCHAR() ,
EXIT_MESSAGE VARCHAR() ,
LAST_UPDATED DATETIME,
constraint JOB_INST_EXEC_FK foreign key (JOB_INSTANCE_ID)
references BATCH_JOB_INSTANCE(JOB_INSTANCE_ID)
) ENGINE=InnoDB; CREATE TABLE BATCH_JOB_EXECUTION_PARAMS (
JOB_EXECUTION_ID BIGINT NOT NULL ,
TYPE_CD VARCHAR() NOT NULL ,
KEY_NAME VARCHAR() NOT NULL ,
STRING_VAL VARCHAR() ,
DATE_VAL DATETIME DEFAULT NULL ,
LONG_VAL BIGINT ,
DOUBLE_VAL DOUBLE PRECISION ,
IDENTIFYING CHAR() NOT NULL ,
constraint JOB_EXEC_PARAMS_FK foreign key (JOB_EXECUTION_ID)
references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
) ENGINE=InnoDB; CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY ,
VERSION BIGINT NOT NULL,
STEP_NAME VARCHAR() NOT NULL,
JOB_EXECUTION_ID BIGINT NOT NULL,
START_TIME DATETIME NOT NULL ,
END_TIME DATETIME DEFAULT NULL ,
STATUS VARCHAR() ,
COMMIT_COUNT BIGINT ,
READ_COUNT BIGINT ,
FILTER_COUNT BIGINT ,
WRITE_COUNT BIGINT ,
READ_SKIP_COUNT BIGINT ,
WRITE_SKIP_COUNT BIGINT ,
PROCESS_SKIP_COUNT BIGINT ,
ROLLBACK_COUNT BIGINT ,
EXIT_CODE VARCHAR() ,
EXIT_MESSAGE VARCHAR() ,
LAST_UPDATED DATETIME,
constraint JOB_EXEC_STEP_FK foreign key (JOB_EXECUTION_ID)
references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
) ENGINE=InnoDB; CREATE TABLE BATCH_STEP_EXECUTION_CONTEXT (
STEP_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY,
SHORT_CONTEXT VARCHAR() NOT NULL,
SERIALIZED_CONTEXT TEXT ,
constraint STEP_EXEC_CTX_FK foreign key (STEP_EXECUTION_ID)
references BATCH_STEP_EXECUTION(STEP_EXECUTION_ID)
) ENGINE=InnoDB; CREATE TABLE BATCH_JOB_EXECUTION_CONTEXT (
JOB_EXECUTION_ID BIGINT NOT NULL PRIMARY KEY,
SHORT_CONTEXT VARCHAR() NOT NULL,
SERIALIZED_CONTEXT TEXT ,
constraint JOB_EXEC_CTX_FK foreign key (JOB_EXECUTION_ID)
references BATCH_JOB_EXECUTION(JOB_EXECUTION_ID)
) ENGINE=InnoDB; CREATE TABLE BATCH_STEP_EXECUTION_SEQ (ID BIGINT NOT NULL) ENGINE=MYISAM;
INSERT INTO BATCH_STEP_EXECUTION_SEQ values();
CREATE TABLE BATCH_JOB_EXECUTION_SEQ (ID BIGINT NOT NULL) ENGINE=MYISAM;
INSERT INTO BATCH_JOB_EXECUTION_SEQ values();
CREATE TABLE BATCH_JOB_SEQ (ID BIGINT NOT NULL) ENGINE=MYISAM;
INSERT INTO BATCH_JOB_SEQ values();
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch" //这里batch的xsd
xmlns:bean="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd"> <!-- 作业仓库 -->
<job-repository id="jobRepository" data-source="dataSource"
transaction-manager="transactionManager" isolation-level-for-create="SERIALIZABLE"
table-prefix="BATCH_" max-varchar-length=""
/> <!-- 作业调度器 -->
<bean:bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<bean:property name="jobRepository" ref="jobRepository"/>
</bean:bean> <!-- 事务管理器 -->
<bean:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<bean:property name="dataSource" ref="dataSource" />
</bean:bean> <!-- 数据源 可以引入properties 做成可配-->
<bean:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<bean:property name="driverClassName">
<bean:value>com.mysql.jdbc.Driver</bean:value>
</bean:property>
<bean:property name="url">
<bean:value>jdbc:mysql://127.0.0.1:3306/batch</bean:value>
</bean:property>
<bean:property name="username" value="root"></bean:property>
<bean:property name="password" value="root"></bean:property>
</bean:bean>
</bean:beans>

这里拿spring batch一书上面例子说明应用场景:

如图 这种情况  下面简单的实现:

1.定义信用卡实体

/**
*
*/
package com.juxtapose.example.ch02; /**
* 信用卡对账单模型.<br>
* @author bruce.liu(mailto:jxta.liu@gmail.com)
* 2013-1-6下午09:56:02
*/
public class CreditBill {
private String accountID = ""; /** 银行卡账户ID */
private String name = ""; /** 持卡人姓名 */
private double amount = ; /** 消费金额 */
private String date; /** 消费日期 ,格式YYYY-MM-DD HH:MM:SS*/
private String address; /** 消费场所 **/ public String getAccountID() {
return accountID;
}
public void setAccountID(String accountID) {
this.accountID = accountID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
} /**
*
*/
public String toString(){
StringBuffer sb = new StringBuffer();
sb.append("accountID=" + getAccountID() + ";name=" + getName() + ";amount="
+ getAmount() + ";date=" + getDate() + ";address=" + getAddress());
return sb.toString();
}
}

2. 定义processor 这里只是打印  

import org.springframework.batch.item.ItemProcessor;

public class CreditBillProcessor implements
ItemProcessor<CreditBill, CreditBill> { public CreditBill process(CreditBill bill) throws Exception {
System.out.println(bill.toString());
return bill;
}
}

第三 :

<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:bean="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">
<bean:import resource="classpath:ch02/job-context.xml"/>
<job id="billJob">
<step id="billStep">
<tasklet transaction-manager="transactionManager">
<chunk reader="csvItemReader" writer="csvItemWriter"
processor="creditBillProcessor" commit-interval=""> //每两行插入一次
</chunk>
</tasklet>
</step>
</job> <job id="testJob">
<step id="testStep">
<tasklet transaction-manager="transactionManager">
<chunk reader="csvItemReader" writer="csvItemWriter"
processor="creditBillProcessor" commit-interval="">
</chunk>
</tasklet>
</step>
</job>
<!-- 读取信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<bean:property name="resource"
value="classpath:ch02/data/credit-card-bill-201303.csv"/> //读取的文件名称
<bean:property name="lineMapper">
<bean:bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<bean:property name="lineTokenizer" ref="lineTokenizer"/>
<bean:property name="fieldSetMapper">//映射列
<bean:bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<bean:property name="prototypeBeanName" value="creditBill">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
<!-- lineTokenizer -->
<bean:bean id="lineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<bean:property name="delimiter" value=","/> //分隔符
<bean:property name="names">
<bean:list> //列名
<bean:value>accountID</bean:value>
<bean:value>name</bean:value>
<bean:value>amount</bean:value>
<bean:value>date</bean:value>
<bean:value>address</bean:value>
</bean:list>
</bean:property>
</bean:bean> <!-- 写信用卡账单文件,CSV格式 -->
<bean:bean id="csvItemWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter"
scope="step">
<bean:property name="resource" value="file:target/ch02/outputFile.csv"/>
<bean:property name="lineAggregator">
<bean:bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<bean:property name="delimiter" value=","></bean:property>
<bean:property name="fieldExtractor">
<bean:bean
class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<bean:property name="names"
value="accountID,name,amount,date,address">
</bean:property>
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <bean:bean id="creditBill" scope="prototype"
class="com.juxtapose.example.ch02.CreditBill">
</bean:bean>
<bean:bean id="creditBillProcessor" scope="step"
class="com.juxtapose.example.ch02.CreditBillProcessor">
</bean:bean>
</bean:beans>

4.测试

/**
*
*/
package test.com.juxtapose.example.ch02; import java.util.LinkedHashMap;
import java.util.Map; import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
*
* @author bruce.liu(mailto:jxta.liu@gmail.com)
* 2013-2-28下午08:34:48
*/
public class JobLaunch { /**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"ch02/job/job.xml");
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("billJob");
try {
//这里只是测试参数
Map<String,JobParameter> map = new LinkedHashMap<String,JobParameter>();
JobParameter jb = new JobParameter("aa",true);
map.put("aa", jb);
JobParameters jbs = new JobParameters(map);
JobExecution result = launcher.run(job, jbs);
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

  

结果如上。  大功告成。

下篇将表结构和各个组件的实现和扩展。

20170818 5.00

Spring batch学习 (1)的更多相关文章

  1. Spring Batch学习笔记三:JobRepository

    此系列博客皆为学习Spring Batch时的一些笔记: Spring Batch Job在运行时有很多元数据,这些元数据一般会被保存在内存或者数据库中,由于Spring Batch在默认配置是使用H ...

  2. Spring Batch学习笔记二

    此系列博客皆为学习Spring Batch时的一些笔记: Spring Batch的架构 一个Batch Job是指一系列有序的Step的集合,它们作为预定义流程的一部分而被执行: Step代表一个自 ...

  3. spring batch学习笔记

    Spring Batch是什么?       Spring Batch是一个基于Spring的企业级批处理框架,按照我师父的说法,所有基于Spring的框架都是使用了spring的IoC特性,然后加上 ...

  4. Spring Batch学习

    今天准备研究下Spring Batch,然后看了一系列资料,如下还是比较好的教程吧. 链接: http://www.cnblogs.com/gulvzhe/archive/2011/12/20/229 ...

  5. Spring Batch学习笔记(一)

    Spring Batch简介 Spring Batch提供了可重复使用的功能,用来处理大量数据.包括记录.跟踪,事务管理,作业处理统计,作业重启,跳过和资源管理. 此外还提供了更高级的技术服务和功能, ...

  6. Spring batch学习 详细配置解读(3)

    第一篇讲到普通job 配置 那么spring  batch 给我们提供了丰富的配置,包括定时任务,校验,复合监听器,父类,重启机制等. 下面看一个动态设置读取文件的配置 1.动态文件读取 <?x ...

  7. Spring batch学习 持久化表结构详解(2)

    #接上一篇 这一篇讲一下持久化需要表 batch_job_execution, batch_job_execution_context, batch_job_execution_params, bat ...

  8. Maven+Spring Batch+Apache Commons VF学习

    Apache Commons VFS资料:例子:http://www.zihou.me/html/2011/04/12/3377.html详细例子:http://p7engqingyang.iteye ...

  9. spring batch批处理框架学习

    内如主要来自以下链接: http://www.importnew.com/26177.html http://www.infoq.com/cn/articles/analysis-of-large-d ...

随机推荐

  1. BooStrap4文档摘录 Utilities

    border:可以用原生css实现效果.❌没看 clearfix, float,  ✅ close icon ✅ colors ✅ display✅  各种显示的格式. embed ✅ <ifr ...

  2. Leetcode 18

    class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int ta ...

  3. C#接口作用

    1.C#接口的作用 : C#接口是一个让很多初学C#者容易迷糊的东西,用起来好像很简单,定义接口,里面包含方法,但没有方法具体实现的代码,然后在继承该接口的类里面要实现接口的所有方法的代码,但没有真正 ...

  4. C#属性升级版--自动属性-chapter 3 P34-36

    使用C#属性,能够通过将数据与它的设置和检索方法分离的方式公开类中的一段数据.   例如:   namespace LanguageFeatures { public class Product { ...

  5. 使用PMD进行代码审查(转)

    原文地址:使用PMD进行代码审查 很久没写博客了,自从上次写的设计模式的博客被不知名的鹳狸猿下架了一次之后兴趣大减,那时候就没什么兴致写博客了,但是这段时间还没有停下来,最近也在研究一些其他的东西,目 ...

  6. DBMS_LOB的简单用法以及释放DBMS_LOB生成的临时CLOB内存

    dbms_lob包(一) dbms_lob包(二) 如何释放DBMS_LOB.CREATETEMPORARY的空间 Temporary LOB导致临时表空间暴满. oracle数据库中的大对象1——永 ...

  7. Git标签(版本)管理

    列出当前所有的标签 git tag     可以搜索特定的标签,例如你只想看稳定版相关的 git tag -l "*.stable"   给当前commit打标签(设定版本) gi ...

  8. 二叉树的基本功能实现方法(C++)

    假设:有一个n个元素的完全二叉树,为了使其成为满二叉树,补全没有孩子的节点是的除了叶节点所有节点都有两个孩子,即最低层皆为-1. 例1: 1 2   3 4 5 -1 6 -1  -1     -1 ...

  9. 都是用 DllImport?有没有考虑过自己写一个 extern 方法?

    你做 .NET 开发的时候,一定用过 DllImport 这个特性吧,这货是用于 P/Invoke (Platform Invoke, 平台调用) 的.这种 DllImport 标记的方法都带有一个 ...

  10. .NET工具软件收集

    ======================== ILSPY        官网:  http://ilspy.net/ .NET Reflector                官网:http:/ ...