内容简介:Spring Batch提供了许多有用的功能,例如:•日志记录和跟踪
Spring Batch 是一个用于创建健壮的批处理应用程序的完整框架。您可以创建可重用的函数来处理大量数据或任务,通常称为批量处理。
Spring Batch提供了许多有用的功能,例如:
•日志记录和跟踪
•事务管理
•作业统计
•管理流程; 例如,通过重新启动作业,跳过步骤和资源管理
•管理Web控制台
该框架旨在管理大量数据并使用分区功能实现高性能批处理。本文将介绍一个简单的项目,解释Spring Batch的每个主要组件。
如Spring Batch文档中所述,使用该框架的最常见方案如下:
•定期提交批处理
•并行处理作业的并发批处理
•分阶段,企业消息驱动处理
•大型并行批处理
•手动或故障后的计划重新启动
•依赖步骤的顺序处理(扩展到工作流程驱动的批处理)
•部分处理:跳过记录(例如,回滚时)
•整批事务:对于批量小或现有存储过程的情况/脚本
在企业应用程序中,处理数百万条记录(数据)或从源读取的需求非常普遍。此源可能包含具有多个记录(如CSV或TXT文件)或数据库表的大型文件。在每个记录中,通常应用一些业务逻辑,执行验证或转换,并完成任务,将结果写入另一种输出格式(例如,数据库或文件)。
Spring Batch提供了一个完整的框架来实现这种要求,最大限度地减少人为干预。
您将查看Spring批处理的基本概念:
•一个Job作业封装批处理过程,并且必须包含一个或多个Step步骤。每个步骤可以按顺序运行,并行运行或分区。
•Step步骤是Job作业的序列组成部分。
•JobLauncher负责执行作业的启动执行,JobExecution是一个Job的运行上下文。
•JobRepository是JobExecution的元数据存储库。
下面使用Spring Batch创建一个简单的作业示例,以了解它的工作原理。
首先,创建一个简单的 Java 项目并包含spring-batch依赖项。为此,使用其初始化程序(https://start.spring.io)创建Spring Boot应用程序。
添加Spring Batch的依赖项。您可以通过在依赖项框内的搜索栏中键入Spring Batch,然后单击Enter来完成此操作。带有单词Batch的绿色框将显示在所选的依赖项部分中。完成此操作后,单击Generate Project按钮。
pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-test</artifactId> <scope>test</scope> </dependency>
配置嵌入数据源。出于测试目的,请使用HSQL(http://hsqldb.org/):
<dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope> </dependency>
现在,您需要将@EnabledBatchProcessing和@Configuration注释添加到应用程序:
@SpringBootApplication
@EnableBatchProcessing
@Configuration
<b>public</b> <b>class</b> SimpleBatchApplication {
接下来,在这个应用入口类中使用JobBuildFactory类和一个任务进程设置第一个作业,使用StepBuilderFactory类:
@Autowired privateJobBuilderFactoryjobBuilderFactory; @Autowired privateStepBuilderFactorystepBuilderFactory;
然后,Job方法将启动它:
@Bean
<b>public</b> Job job(Step ourBatchStep) throws Exception {
returnjobBuilderFactory.get(<font>"jobPackPub1"</font><font>)
.incrementer(<b>new</b> RunIdIncrementer())
.start(ourBatchStep)
.build();
}
</font>
创建作业后,将新任务(步骤)添加到作业:
@Bean
<b>public</b> Step ourBatchStep() {
returnstepBuilderFactory.get(<font>"stepPackPub1"</font><font>)
.tasklet(<b>new</b> Tasklet() {
publicRepeatStatus execute(StepContribution contribution,
ChunkContextchunkContext) {
<b>return</b> <b>null</b>;
}
})
.build();
}
</font>
以下代码显示了应用程序类的完整代码:
@EnableBatchProcessing
@SpringBootApplication
@Configuration
<b>public</b> <b>class</b> SimpleBatchApplication {
<b>public</b> <b>static</b> <b>void</b> main(String[] args) {
SpringApplication.run(SimpleBatchApplication.<b>class</b>, args);
}
@Autowired
privateJobBuilderFactoryjobBuilderFactory;
@Autowired
privateStepBuilderFactorystepBuilderFactory;
@Bean
<b>public</b> Step ourBatchStep() {
returnstepBuilderFactory.get(<font>"stepPackPub1"</font><font>)
.tasklet(<b>new</b> Tasklet() {
publicRepeatStatus execute
(StepContribution contribution,
ChunkContextchunkContext) {
<b>return</b> <b>null</b>;
}
})
.build();
}
@Bean
<b>public</b> Job job(Step ourBatchStep) throws Exception {
returnjobBuilderFactory.get(</font><font>"jobPackPub1"</font><font>)
.incrementer(<b>new</b> RunIdIncrementer())
.start(ourBatchStep)
.build();
}
}
</font>
为了检查一切正常,请运行该应用程序。为此,请在命令行上执行以下命令:
mvnspring-boot:run
或者,您可以通过运行Maven来构建应用程序:
mvn install
运行这个构建的jar:
java -jar target/simple-batch-0.0.1-SNAPSHOT.jar
注意控制台输出。为此,运行名为jobPackPub1的作业并将该bean作为stepPackPub1.Now执行,更详细地查看以下步骤背后的组件:
•ItemReader表示检索步骤的输入
•ItemProcessor表示项目的业务处理
•ItemWriter表示步骤的输出
下图:
现在,您可以使用ItemReader,ItemProcessor和ItemWriter完成示例。可以通过使用这些组件实现Spring Batch管道和过滤器架构。
以在 此处 找到本文的源代码
以上所述就是小编给大家介绍的《Spring Batch批处理简介》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 数据批处理神器:Spring Batch(一)简介及使用场景
- 动画骨骼节点批处理
- Spark 持续流处理和微批处理的对比
- Jet 4.5 发布,分布式批处理和流处理引擎
- 注册中心 Eureka 源码解析 —— 任务批处理
- java – JMS和Spring批处理
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Making Things See
Greg Borenstein / Make / 2012-2-3 / USD 39.99
Welcome to the Vision Revolution. With Microsoft's Kinect leading the way, you can now use 3D computer vision technology to build digital 3D models of people and objects that you can manipulate with g......一起来看看 《Making Things See》 这本书的介绍吧!