导读
最近一个项目使用了Spring Batch框架做了一个对账文件的批处理。当时网上资料很少,官网的文档也是只讲了一些入门的基础知识,在实际开发过程中遇到了很多的问题。因此,特将自己学习、应用Spring Batch的过程总结成一个个小实例写成随笔。
本文项目地址
Spring Batch简介
Spring Batch是一个轻量级的,完全面向Sprin的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供大量的,可重复的数据处理功能,包括日志记录/跟踪,事务管理,作业处理统计工作重新启动、跳过,和资源管理等重要功能。
业务方案
- 批处理定期提交。
- 并行批处理:并行处理工作。
- 企业消息驱动处理
- 大规模的并行处理
- 手动或是有计划的重启
- 局部处理:跳过记录(如:回滚)
技术目标:
- 利用Spring编程模型:使程序员专注于业务处理,让Spring框架管理流程。
- 明确分离批处理的执行环境和应用。
- 提供核心的,共通的接口。
- 提供开箱即用(out of the box)的简单的默认的核心执行接口。
- 提供Spring框架中配置、自定义、和扩展服务。
- 所有存在的核心服务可以很容的被替换和扩展,不影响基础层。
- 提供一个简单的部署模式,利用Maven构建独立的Jar文件。
Spring Batch的结构:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-btENl3PX-1648430036364)(http://p6neued6m.bkt.clouddn.com/spring-batch-acrh.jpg)]
这种分层结构有三个重要的组成部分:应用层、核心层、基础架构层。应用层包含所有的批处理作业,通过Spring框架管理程序员自定义的代码。核心层包含了Batch启动和控制所需要的核心类,如:JobLauncher、Job和step等。应用层和核心层建立在基础构架层之上,基础构架层提供共通的读(ItemReader)、写(ItemWriter)、和服务(如RetryTemplate:重试模块。可以被应用层和核心层使用)。
框架流程简单介绍
Spring Batch流程介绍:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uBULrje6-1648430036365)(http://p6neued6m.bkt.clouddn.com/spring-batch-liucheng.png)]
上图描绘了Spring Batch的执行过程。说明如下:
每个Batch都会包含一个Job。Job就像一个容器,这个容器里装了若干Step,Batch中实际干活的也就是这些Step,至于Step干什么活,无外乎读取数据,处理数据,然后将这些数据存储起来(ItemReader用来读取数据,ItemProcessor用来处理数据,ItemWriter用来写数据) 。JobLauncher用来启动Job,JobRepository是上述处理提供的一种持久化机制,它为JobLauncher,Job,和Step实例提供CRUD操作。
外部控制器调用JobLauncher启动一个Job,Job调用自己的Step去实现对数据的操作,Step处理完成后,再将处理结果一步步返回给上一层,这就是Batch处理实现的一个简单流程。
Step执行过程:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VOE6CaZT-1648430036367)(http://p6neued6m.bkt.clouddn.com/spring-batch-step.png)]
从DB或是文件中取出数据的时候,read()操作每次只读取一条记录,之后将读取的这条数据传递给processor(item)处理,框架将重复做这两步操作,直到读取记录的件数达到batch配置信息中”commin-interval”设定值的时候,就会调用一次write操作。然后再重复上图的处理,直到处理完所有的数据。当这个Step的工作完成以后,或是跳到其他Step,或是结束处理。
这就是一个SpringBatch的基本工作流程。
下次,将通过“Hello World”实例,与大家共同探讨SpringBatch的具体应用和实现。
Sample(Hello World)
通过上面对Spring Batch的介绍,大家应该已经对Spring Batch有个初步的概念了。现在将通过一个”Hello World!”实例,和大家一起探讨关于Spring Batch的一些基本配置和实现。使大家从开发的角度对Spring Batch有一个真切的体会。
说明:
-
本实例使用的是spring-batch 3.0.7.RELEASE
-
本实例没有像前面讲的那样配置ItemReader、ItemProcessor和ItemWriter,而是之间在Step中调用Tasklet,由Tasklet完成”Hello World!”的输出。
工程结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7XZOqKXP-1648430036368)(http://p6neued6m.bkt.clouddn.com/spring-batch-helloworld.png)]
HelloJobLaunch.java类用来启动Bath,WriteTasklet.java用来完成输出工作。application.xml用来配置一些Spring信息,spring-batch-hello.xml配置Job信息。
application.xml文件配置如下:
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byName">
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
<context:annotation-config/>
<context:component-scan base-package="com.cheng.springbatch"/>
<import resource="classpath:spring-datasource.xml"/>
<import resource="classpath:spring-batch-hello.xml"/>
<!--<import resource="classpath:spring-batch-csv.xml"/>-->
<!--<import resource="classpath:spring-batch-xml.xml"/>-->
<!--<import resource="classpath:spring-batch-fixed.xml"/>-->
<!--<import resource="classpath:spring-batch-multi.xml"/>-->
<!--<import resource="classpath:spring-batch-jdbc.xml"/>-->
</beans>
jobLauncher负责batch的启动工作,jobRepository负责job的整个运行过程中的CRUD操作,transactionManager负责事务的管理操作。
spring-batch-hello.xml文件配置如下:
<?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:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch.xsd">
<!--定义Jobs-->
<batch:job id="helloWorldJob">
<batch:step id="step_hello" next="step_world">
<batch:tasklet ref="hello" transaction-manager="transactionManager"/>
</batch:step>
<batch:step id="step_world">
<batch:tasklet ref="world" transaction-manager="transactionManager"/>
</batch:step>
</batch:job>
<bean id="hello" class="com.cheng.springbatch.hello.WriteTasklet">
<property nam