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框架为基础,使开发者更容易的访问 ...
随机推荐
- 简单的zip压缩和解压缩
其实像这样的php拓展很多,只是项目中没怎么用到: <?php $zip = new ZipArchive(); $filename = "./test112.zip"; / ...
- php加密总结
1.md5加密,加密之后是32位的字符串 2.sha1加密, 加密之后是40位的字符串 3.crypt加密, 加密之后是13位的字符串 上面是不可逆的 可逆的如下urlencode --> ur ...
- linux下开启https
1.在线安装mod_ssl yum -y install mod_ssl 查看openssl 是否安装成功 rpm -qa |grep openssl 2.建立服务器密钥 openssl genrsa ...
- 【C#】反编译C#应用程序
下面的使用的使用是笔者使用的一个简单的程序,就是弹出一个消息框. 这里通过 ildasm 反编译 应用程序得到 il 文件,然后对 il 文件进行修改,修改内容后,再通过ilasm编译为应用程序.il ...
- CheckStyle检查规则中文翻译
本文主要介绍CheckStyle 的各个规则配置项目,这个版本的CheckStyle将样式规则分为了以下十六种类型共138条规则: 官方文档地址:http://checkstyle.sourcefor ...
- Spark弹性分布式数据集RDD
RDD(Resilient Distributed Dataset)是Spark的最基本抽象,是对分布式内存的抽象使用,实现了以操作本地集合的方式来操作分布式数据集的抽象实现.RDD是Spark最核心 ...
- 【web】a标签点击时跳出确认框
[web]a标签点击时跳出确认框 https://blog.csdn.net/michael_ouyang/article/details/52765575需求如下: 在跳转链接前,需要判断该用户是否 ...
- appium简明教程(4)——appium client的安装
appium client是对webdriver原生api的一些扩展和封装.它可以帮助我们更容易的写出用例,写出更好懂的用例. appium client是配合原生的webdriver来使用的,因此二 ...
- 10分钟轻松设置出 A+ 评分的 HTTP/2 网站
前言 其实 HTTP/2 应该是 2015 年的老话题了(2015 年 5 月 14 日 HTTP/2 协议正式版的发布),但是 2018 年都到了很多网站依旧没有使用,作为新一代互联网协议,HTTP ...
- centos中添加php扩展pdo_mysql步骤
本文内容是以 CentOS 为例,红帽系列的 Linux 方法应该都是如此,下面就详细说明步骤,在这里严重鄙视哪些内容??隆⑺档脑悠咴影说挠泄 PDO 编译安装的文章. 1.进入 PHP 的软件包 p ...