Spring Batch并行与扩展
1. 概述
Spring Batch提供了多种方式用于处理并行,提高性能。主要分为2大类:
- 单个进程,多线程
- 多个进程
因此,可以细分为以下几类:
- 多线程Step(Multi-thread Step,single process)
- 并行Step(Parallel Steps, single process )
- Remote Chunking of Step( multi process)
- Partitioning a step(single or multi process)
2. Multi-Thread Step
最直接的方式是给Step配置一个TaskExecutor
<step id="loading">
<tasklet task-executor="taskExecutor">...</tasklet>
</step>
此时,taskExecutor的线程并行来执行Item处理(统一item的read,process,write在同一个线程中执行)。可以限制TaskExecutor的阈值(默认为4):
<step id="loading"> <tasklet
task-executor="taskExecutor"
throttle-limit="20">...</tasklet>
</step>
需要注意的是,在多线程Step中,需要确保Reader、Processor和Writer是线程安全的,否则容易出现并发问题。Spring Batch提供的大部分组件都是非线程安全的,他们都保存有部分状态信息,主要是为了支持任务重启。
因此,使用多线程Step的核心任务是实现无状态化,例如不保存当前读取的item的cursor,而是同item的flag字段来区分item是否被处理过,已经被处理过的下次重启的时候,直接被过滤掉。
多线程Step实现的是单个Step的多线程化。
3. Parallel Steps
如果多个Step没有先后关系,可以并行执行,这是通过split和flow来实现的:
<job id="job1">
<split id="split1" task-executor="taskExecutor" next="step4">
<flow>
<step id="step1" parent="s1" next="step2"/>
<step id="step2" parent="s2"/>
</flow>
<flow>
<step id="step3" parent="s3"/>
</flow>
</split>
<step id="step4" parent="s4"/>
</job> <beans:bean id="taskExecutor" class="org.spr...SimpleAsyncTaskExecutor"/>
该种模式提供的是多个Step的并行处理。
4. Remote Chunking
Remote chunking 的示意图如下: 
Master为单个进程,因此只有在处理所需要的时间远远大于读取所需要的时间的时候,这个方式才适用,否则Master容易成为瓶颈。
Master是一个常规的Step实现,只不过它的ItemWriter知道如何将Items分块,并发送到中间件(例如JMS),通过实现ChunkProvider接口来实现。
public interface ChunkProvider<T>{
Chunk<T> provide(StepContribution contribution) throws Exception;
void postProcess(StepContribution contribution, Chunk<T> chunk);
}
Slave则充当中间件的Listener,通过ItemProcessor和ItemWriter来实现item处理,具体的是通过实现ChunkProcessor接口
public interface ChunkProcessr<T> {
void process(StepContribution contribution,Chunk<T> chunk) throws Exception;
}
可以看到,remote chunking实现的是(Processor、Writer)的并行化。分区不需要对数据源的结构有很明确的了解。
5. Partitioning
Step分区处理示意图如下: 
一个分区配置如下:
<step id="step1.master">
<partition step="step1" partitioner="partitioner">
<handler grid-size="10" task-executor="taskExecutor"/>
</partition>
</step> <step id="step1">
<tasklet>
<chunk reader="" writer"" processor="" .../>
</tasklet>
</step>
主要包括2个步骤:
1. 数据分区
2. 分区处理
具体的分区执行流程如下: 
PartitionHandler
其中PartitionHandler知道集群环境,根据下面要介绍的Splitter进行分区,发送执行请求(通过WebService ,RMI等方式) 并收集执行结果,聚合,最终反馈给Job。Spring Batch提供了一个同一台机器上的Handler实现,在同一机器上创建多个Step Execution。
<step id="step1.master">
<partition step="step1" handler="handler"/>
</step> <bean class="org.spr...TaskExecutorPartitionHandler">
<property name="taskExecutor" ref="taskExecutor"/>
<property name="step" ref="step1" />
<property name="gridSize" value="10" />
</bean>
Partitioner
Partitioner负责生成执行上下文,作为Step Execution的输入参数,其接口定义如下:
public interface Partitioner {
Map<String, ExecutionContext> partition(int gridSize);
}
返回结果中Map的key,是一个唯一的名字,常见的实现方式是step_name + counter。或者通过PartitioneNameProvider来提供。 名字关联到对应的执行上下文。ExecutionContext只是一个key/value容器,因此它可能包含主键范围,行数等信息。
StepExecutionSplitter
Partitioner生成的ExecutionContext,经过StepExecutionSplitter处理之后形成StepExecution,然后交给Handler处理。StepExecutionSplitter接口定义如下:
public interface StepExecutionSplitter {
String getStepName();
Set<StepExecution> split(StepExecution stepExecution , int gridSize)
throws JobExecutionException;
}
通常,Slave中的Step配置都是相同的,他们通过获取Partitioner划分好的ExecutionContext,获取Step的输入参数,动态绑定到Step中。例如划分的情况如下表:
| step execution name(key) | ExecutionContext(value) |
|---|---|
| filecopy:partition0 | file_name=/home/data/0 |
| filecopy:partition1 | file_name=/home/data/1 |
| filecopy:partition2 | file_name=/home/data/2 |
然后该文件名被绑定到Step的组件中:
<bean id="itemReader" scope="step"
class="org.spr...MultiResourceItemReader">
<property name="resource" value="#{stepExecutionContext[file_name]}/*"/>
</bean>
整个具体流程如下:
可以看出,Patitioning提供的是(Reader、Processor、Writer)的并行化。分区模式需要对数据源的结构有一定的了解,比如知道主键范围。
本文转自:https://blog.csdn.net/bingduanlbd/article/details/50989664
Spring Batch并行与扩展的更多相关文章
- Spring Batch在大型企业中的最佳实践
在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...
- Spring Batch学习笔记二
此系列博客皆为学习Spring Batch时的一些笔记: Spring Batch的架构 一个Batch Job是指一系列有序的Step的集合,它们作为预定义流程的一部分而被执行: Step代表一个自 ...
- Spring Batch 中文参考文档 V3.0.6 - 1 Spring Batch介绍
1 Spring Batch介绍 企业领域中许多应用系统需要采用批处理的方式在特定环境中运行业务操作任务.这种业务作业包括自动化,大量信息的复杂操作,他们不需要人工干预,并能高效运行.这些典型作业包括 ...
- Spring Batch 批处理框架
<Spring Batch 批处理框架>基本信息作者: 刘相 出版社:电子工业出版社ISBN:9787121252419上架时间:2015-1-24出版日期:2015 年2月开本:16开页 ...
- Spring Batch实践
Spring Batch在大型企业中的最佳实践 在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后 ...
- 图书简介:Spring Batch批处理框架
大数据时代批处理利器,国内首度原创解析Spring Batch框架. 内容简介: <Spring Batch 批处理框架>全面.系统地介绍了批处理框架Spring Batch,通过详尽的实 ...
- Spring Batch 专题
如今微服务架构讨论的如火如荼.但在企业架构里除了大量的OLTP交易外,还存在海量的批处理交易.在诸如银行的金融机构中,每天有3-4万笔的批处理作业需要处理.针对OLTP,业界有大量的开源框架.优秀的架 ...
- spring batch批量处理框架
spring batch精选,一文吃透spring batch批量处理框架 前言碎语 批处理是企业级业务系统不可或缺的一部分,spring batch是一个轻量级的综合性批处理框架,可用于开发企业信息 ...
- Spring Batch 简介
Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统.Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问 ...
随机推荐
- php支付宝开发
1.下载对应的接口demo,注意退款有无密和有密 2.根据接口代码编写,要注意notify_url和return_url不能带有额外参数,并且notify_url中途不能含有跳转,否则post数据会丢 ...
- redis常用性能分析命令
一.连接 src/redis-cli -h 10.20.137.141 -p 6379 >auth 123456789 src/redis-cli -h 10.20.137.141 -p 637 ...
- SharePoint 2013 Troubleshooting——启用 Developer Dashboard
SharePoint 2010的管理员和开发者可能对SharePoint Developer Dashboard(开发人员仪表盘)很熟悉.在SharePoint 2013这个工具已经被大范围的改写了, ...
- Flowable 的event介绍
1 事件分为两种,一种是抛出:当流程执行到这时,抛出一个事件:另一种是捕获:当流程执行到这时,他就会等待一个事件的发生. 一个事件需要有事件定义,否则不会做任何“特殊”的事.对于一个流程实例,一个开始 ...
- 微信小程序图片宽100%显示并且不变形
<view class="meiti" style="background-color:red;"> <image src="htt ...
- unity, Collider2D.attachedRigidbody
boss根节点上挂RigidBody2D(且boss根节点以下任何子节点均不挂RigidBody2D),boss腿部骨骼节点挂collider2D,标签为"bossLeg",bos ...
- Windows 上 怎么安装 install elasticsearch plugin
D:\elasticsearch-5.2.1\bin>elasticsearch-plugin install file:///D:/elasticsearch -5.2.1/bin/elast ...
- POJ 1636 Prison rearrangement DFS+0/1背包
题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS Memor ...
- ASP.NET MVC中的cshtml页面中的下拉框的使用
ASP.NET MVC中的cshtml页面中的下拉框的使用 用上@Html.DropDownList 先记下来..以做备忘...
- ios开发中的一些小技巧
1.如果在程序中想对某张图片进行处理的话(得到某张图片的一部分)可一用以下代码: UIImage *image = [UIImage imageNamed:filename]; CGImageRe ...