深入了解 Spring 之 Spring Batch 框架

快来打我* 2024-04-08 12:34 128阅读 0赞

一. 概述

spring batch 是 spring 提供的一个数据处理框架,其功能包括记录/跟踪,事务管理,作业统计,作业重启,跳过和资源管理等。它还提供了更高级的技术服务和功能,通过优化和分区技术实现极高容量和高性能的批处理作业。

首先会对其框架所涉及到概念进行讲解,接着对其框架大体原理进行解读。

二. 概念及原理

  1. JobLauncher

91447bf9b2bb4c5981155483e957e8bf.png

该接口是启动任务的主要入口,其入参是 Job 实例,以及 Job 对应的参数信息。其实现类为 SImpleJobLauncher 类,其里面有两个关键的属性成员:

·jobRepository 保存任务或者检索任务的信息;

·taskExecutor 任务执行器,主要是同步执行还是异步执行任务;

流程图如下:

b8274869c39d4a719eb8f90724f32e5b.png

每个 Step 的运行状态都有哪些,可以查看 BatchStatus 枚举类,后面有对其状态进行介绍。

  1. Job

我们执行的任务,该任务就是 Job 接口下的实现类,其类图如下:

012c07dc8cf849a0bfdd878180d0dd46.png

AbstractJob 的属性介绍:

·restartable 是否允许重跑

·name 任务名

·listener 监听器

·jobParametersIncrementer 获取下一个 JobParameters 对象,其实质是对 JobParameters 上添加一个自增或者随机的 KV 对象;

·jobParametersValidator 是对 JobParameters 校验;

·stepHandler 执行 Step 的适配器。

下面的子类是根据 Step 的组成规则不一样。

·SimpleJob 是按照“顺序式”执行 Step。

·FlowJob 按照“流式”执行 Step,其具体执行过程由 Flow 接口的子类来实现。

一个 Job 的执行过程如下:

0a28a1aca39b4d01b0ea7aedba6f91f3.png

SimpleJob 子类的 doExecute 方法








protected void doExecute(JobExecution execution) throws JobInterruptedException, JobRestartException,
StartLimitExceededException {
StepExecution stepExecution = null;
for (Step step : steps) {
//遍历集合中的step,通过StepHandler去执行Step
stepExecution = handleStep(step, execution);
if (stepExecution.getStatus() != BatchStatus.COMPLETED) {
break;
}
}
if (stepExecution != null) {
//设置执行完状态以及退出状态
execution.upgradeStatus (stepExecution.getStatus());
execution.setExitStatus (stepExecution.getExitStatus());
}
}

FlowJob 子类的 doExecute 方法








protected void doExecute (final JobExecution execution) throws JobExecutionException {
try {
JobFlowExecutor executor = new JobFlowExecutor (getJobRepository(),
new SimpleStepHandler (getJobRepository()), execution);
//通过Flow执行
executor.updateJobExecutionStatus (flow.start(executor).getStatus());
}
catch (FlowExecutionException e) {
if (e.getCause() instanceof JobExecutionException) {
throw (JobExecutionException) e.getCause();
}
throw new JobExecutionException (“Flow execution ended unexpectedly”, e);
}
}

2.1 JobExecution

从 SimpleLauncher 流程图中以及 Job 的流程图中,一直都有 JobExecution 影子存在;JobExecution 是记录 Job 执行过程中的信息;JobExecution 类含有的关键属性如下:

2c15b388c41b4803b41ce6b9d1cabbf3.png

·JobParameters 记录 Job 开始运行时传递过来的参数

·stepExecutions 记录 Job 中每个 Step 的运行过程中的信息

发表评论

表情:
评论列表 (有 0 条评论,128人围观)

还没有评论,来说两句吧...

相关阅读