深入了解 Spring 之 Spring Batch 框架
一. 概述
spring batch 是 spring 提供的一个数据处理框架,其功能包括记录/跟踪,事务管理,作业统计,作业重启,跳过和资源管理等。它还提供了更高级的技术服务和功能,通过优化和分区技术实现极高容量和高性能的批处理作业。
首先会对其框架所涉及到概念进行讲解,接着对其框架大体原理进行解读。
二. 概念及原理
- JobLauncher
该接口是启动任务的主要入口,其入参是 Job 实例,以及 Job 对应的参数信息。其实现类为 SImpleJobLauncher 类,其里面有两个关键的属性成员:
·jobRepository 保存任务或者检索任务的信息;
·taskExecutor 任务执行器,主要是同步执行还是异步执行任务;
流程图如下:
每个 Step 的运行状态都有哪些,可以查看 BatchStatus 枚举类,后面有对其状态进行介绍。
- 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 类含有的关键属性如下:
·JobParameters 记录 Job 开始运行时传递过来的参数
·stepExecutions 记录 Job 中每个 Step 的运行过程中的信息
还没有评论,来说两句吧...