深入了解 Spring 之 Spring Batch 框架

一. 概述

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

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

二. 概念及原理

1. JobLauncher

 

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

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

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

流程图如下:

 

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

2. Job

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

 

AbstractJob 的属性介绍:

·restartable 是否允许重跑

·name 任务名

·listener 监听器

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

·jobParametersValidator 是对 JobParameters 校验;

·stepHandler 执行 Step 的适配器。

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

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

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

一个 Job 的执行过程如下:

 

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 类含有的关键属性如下:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值