Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。
Spring Batch可以提供大量的,可重复的数据处理功能,包括日志记录/跟踪,事务管理,作业处理统计工作重新启动、跳过,和资源管理等重要功能。
业务方案:
1、批处理定期提交。
2、并行批处理:并行处理工作。
3、企业消息驱动处理
4、大规模的并行处理
5、手动或是有计划的重启
6、局部处理:跳过记录(如:回滚)
技术目标:
1、利用Spring编程模型:使程序员专注于业务处理,让Spring框架管理流程。
2、明确分离批处理的执行环境和应用。
3、提供核心的,共通的接口。
4、提供开箱即用(out of the box)的简单的默认的核心执行接口。
5、提供Spring框架中配置、自定义、和扩展服务。
6、所有存在的核心服务可以很容的被替换和扩展,不影响基础层。
7、提供一个简单的部署模式,利用Maven构建独立的Jar文件
Spring batch结构:
这种分层结构有三个重要的组成部分:应用层、核心层、基础架构层。应用层包含所有的批处理作业,通过Spring框架管理程序员自定义的代码。核心层包含了Batch启动和控制所需要的核心类,如:JobLauncher、Job和step等。应用层和核心层建立在基础构架层之上,基础构架层提供共通的读(ItemReader)、写(ItemWriter)、和服务(如RetryTemplate:重试模块。可以被应用层和核心层使用)。
案例分析:
excel文件:aaa.csv
id,name,age,scope 1,aa,44,o 2,dd,55,b 3,ss,66,c
需求:将aaa.csv文件的内容复制到bbb.csv文件中:
一、Spring 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: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-2.2.xsd"
default-autowire="byName">
<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"/>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<batch:job id="job">
<batch:step id="step1">
<batch:tasklet >
<batch:chunk reader="csvItemReader" writer="csvItemWriter" commit-interval="1">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="csvItemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="props/aaa.csv"/>
<property name="lineMapper" ref="dlmaper"/>
</bean>
<bean id="dlmaper" class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer" ref="lineTokenizer"/>
<property name="fieldSetMapper" >
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="student"/>
</bean>
</property>
</bean>
<bean id="student" class="com.jd.finance.platform.accounts.domain.vo.Student"/>
<bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" >
<property name="delimiter" value=","/>
<property name="names" >
<list>
<value>id</value>
<value>name</value>
<value>age</value>
<value>score</value>
</list>
</property>
</bean>
<bean id="csvItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<property name="resource" value="props/bbb.csv"/>
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<property name="delimiter" value=","/>
<property name="fieldExtractor">
<bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
<property name="names" value="name,age,scope"/>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
二、执行Action类
@Controller @RequestMapping("/service") public class OperateAnalyzeServiceAction { private static final Logger log = LoggerFactory.getLogger(OperateAnalyzeServiceAction.class); @Autowired private Job job; @Autowired private SimpleJobLauncher jobLauncher; @RequestMapping(value = "/doExecute", method = RequestMethod.GET) public void doExecute() { try { JobExecution result = jobLauncher.run(job, new JobParameters()); /* 处理结束,控制台打印处理结果 */ System.out.println(result.toString()); } catch (Exception e) { e.printStackTrace(); } } }
public class Student { private String id; private String name; private String age; private String scope; public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } }
<dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-infrastructure</artifactId> <version>2.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <version>2.2.3.RELEASE</version> <scope>test</scope> </dependency>
相关推荐
Spring Batch 实例代码
spring batch批处理框架和对应的源码资源 rar 可以直接运行的
2024最新!一文看懂Spring Batch批处理(大白话版,干货满满), 学习你将收获: 一.系统了解Spring Batch批处理; 二.项目中能熟练使用Spring Batch批处理
SpringBatch 示例小程序源码(内含依赖的jar包) 本示例程序 使用 Spring 3 + Spring Batch 2.2.7
Spring Batch API(Spring Batch 开发文档).CHM。 官网 Spring Batch API,Spring Batch 开发文档
Spring Batch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使得已经使用 Spring 框架的开发者或者企业更容易访问和利用企业服务。 Spring Batch 提供了大量可重用的组件,包括了日志、追踪、事务、...
Spring Boot整合Spring Batch的一个小例子,在网上发现这方面的资源比较少,特此将其上传供大家学习。
最近在研究springBoot+springbatch ,按照官网的实例做了一个实例。 最近在研究springBoot+springbatch ,按照官网的实例做了一个实例。
spring-batch4.0.0 batch spring-batch集成 spring-batch.jar
Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...
Work with all aspects of batch processing in a modern Java environment using a selection of Spring frameworks. This book provides up-to-date examples using the latest configuration techniques based on...
基于Spring Batch的大数据量并行处理 基于Spring Batch的大数据量并行处理
主要给大家介绍了Spring Batch读取txt文件并写入数据库的方法,SpringBatch 是一个轻量级、全面的批处理框架。这里我们用它来实现文件的读取并将读取的结果作处理,处理之后再写入数据库中的功能。需要的朋友可以...
Spring Batch in Action is a comprehensive, in-depth guide to writing batch applications using Spring Batch. Written for developers who have basic knowledge of Java and the Spring lightweight ...
SpringBatch数据库建表语句,存储springBatch批处理过程中需要保存的数据和步骤信息
mybatis、springBatch、mysql、quartz、spring、springMVC 部署说明: 本项目为两个数据库,由一个数据库的表向另外一个数据库的表做数据迁移,其中数据库脚本在:/src/main/resources/sql/下面(其中data_rep中的表...
难得的详细spring batch资料 难得的详细spring batch资料
Spring+Batch+Learning实例
Spring Batch批处理框架Spring Batch批处理框架Spring Batch批处理框架
《Spring Batch 批处理框架》全面、系统地介绍了批处理框架Spring Batch,通过详尽的实战示例向读者展示了Spring Batch框架对大数据批处理的基本开发能力,并对框架的架构设计、源码做了特定的剖析;在帮助读者掌握...