Conditional Flow in Spring Batch

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

CHECK OUT THE COURSE

1. Introduction

We use Spring Batch to compose jobs from multiple steps that read, transform, and write data. If the steps in a job have multiple paths, similar to using an if statement in our code, we say that the job flow is conditional.

In this tutorial, we'll look at two ways to create Spring Batch jobs with a conditional flow.

2. Exit Status and Batch Status

When we specify a conditional step with Spring's Batch framework, we're using the exit status of a step or job. Therefore, we need to understand the difference between Batch Status and Exit Status in our steps and jobs:

  • BatchStatus is an Enum representing the status of a step/job and is used by the Batch framework internally
  • Possible values are: ABANDONED, COMPLETED, FAILED, STARTED, STARTING, STOPPED, STOPPING, UNKNOWN
  • ExitStatus is the status of a step when execution is completed and is used to conditionally determine the flow

By default, the ExitStatus of a step or job is the same as its BatchStatus. We can also set a custom ExitStatus to drive flow.

3. Conditional Flow

Let's say we have an IOT device that sends us measurements. Our device measurements are arrays of integers, and we need to send notifications if any of our measurements contain positive integers.

In other words, we need to send a notification when we detect a positive measurement.

3.1. ExitStatus

Importantly, we use the exit status of a step to drive conditional flow.

To set the exit status of a step, we need to use the StepExecution object's setExitStatus method. In order to do that, we need to create an ItemProcessor that extends ItemListenerSupport and gets the step's StepExecution.

We use this to set our step's exit status to NOTIFY when we find a positive number. When we determine our exit status based on data within the batch job, we can use an ItemProcessor.

Let's look at our NumberInfoClassifier to see the three methods we need:

  1. public class NumberInfoClassifier extends ItemListenerSupport<NumberInfo, Integer>
  2. implements ItemProcessor<NumberInfo, Integer> {
  3. private StepExecution stepExecution;
  4. @BeforeStep
  5. public void beforeStep(StepExecution stepExecution) {
  6. this.stepExecution = stepExecution;
  7. this.stepExecution.setExitStatus(new ExitStatus(QUIET));
  8. }
  9. @Override
  10. public Integer process(NumberInfo numberInfo) throws Exception {
  11. return Integer.valueOf(numberInfo.getNumber());
  12. }
  13. @Override
  14. public void afterProcess(NumberInfo item, Integer result) {
  15. super.afterProcess(item, result);
  16. if (item.isPositive()) {
  17. stepExecution.setExitStatus(new ExitStatus(NOTIFY));
  18. }
  19. }
  20. }

Note: we use the ItemProcessor to set the ExitStatus in this example, but we could just as easily do it in our step's ItemReader or ItemWriter.

Finally, when we create our job, we tell our JobBuilderFactory to send notifications for any step that exits with a status of NOTIFY:

  1. jobBuilderFactory.get("Number generator - second dataset")
  2. .start(dataProviderStep)
  3. .on("NOTIFY").to(notificationStep)
  4. .end()
  5. .build();

Also note that when we have additional conditional branches and multiple exit codes, we can add them to our job with the from and on methods of the JobBuilderFacotry:

  1. jobBuilderFactory.get("Number generator - second dataset")
  2. .start(dataProviderStep)
  3. .on("NOTIFY").to(notificationStep)
  4. .from(step).on("LOG_ERROR").to(errorLoggingStep)
  5. .end()
  6. .build();

Now, any time our ItemProcessor sees a positive number, it will direct our job to run the notificationStep, which simply prints a message to System.out:

  1. Second Dataset Processor 11
  2. Second Dataset Processor -2
  3. Second Dataset Processor -3
  4. [Number generator - second dataset] contains interesting data!!

If we had a data set without a positive number, we would not see our notificationStep message:

  1. Second Dataset Processor -1
  2. Second Dataset Processor -2
  3. Second Dataset Processor -3

3.2. Programmatic Branching With JobExecutionDecider

Alternatively, we can use a class that implements JobExecutionDecider to determine job flow. This is especially useful if we have external factors for determining execution flow.

To use this method, we first need to modify our ItemProcessor to remove the ItemListenerSupport interface and @BeforeStep method:

  1. public class NumberInfoClassifierWithDecider extends ItemListenerSupport<NumberInfo, Integer>
  2. implements ItemProcessor<NumberInfo, Integer> {
  3. @Override
  4. public Integer process(NumberInfo numberInfo) throws Exception {
  5. return Integer.valueOf(numberInfo.getNumber());
  6. }
  7. }

Next, we create a decider class that determines the notification status of our step:

  1. public class NumberInfoDecider implements JobExecutionDecider {
  2. private boolean shouldNotify() {
  3. return true;
  4. }
  5. @Override
  6. public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
  7. if (shouldNotify()) {
  8. return new FlowExecutionStatus(NOTIFY);
  9. } else {
  10. return new FlowExecutionStatus(QUIET);
  11. }
  12. }
  13. }

Then, we set up our Job to use the decider in the flow:

  1. jobBuilderFactory.get("Number generator - third dataset")
  2. .start(dataProviderStep)
  3. .next(new NumberInfoDecider()).on("NOTIFY").to(notificationStep)
  4. .end()
  5. .build();

4. Conclusion

In this quick tutorial, we explored two options for implementing conditional flows with Spring Batch. First, we looked at how to use the ExitStatus to control the flow of our job.

Then we took a look at how we can control flow programmatically by defining our own JobExecutionDecider.

As always, the full source code of the article is available over on GitHub.

Spring Batch(0)——控制Step执行流程的更多相关文章

  1. spring batch中控制step的走向

    1.顺序执行step: <job id="job"> <step id="stepA" parent="s1" next= ...

  2. Spring Security 案例实现和执行流程剖析

    Spring Security Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication ...

  3. Spring 文件上传MultipartFile 执行流程分析

    在了解Spring 文件上传执行流程之前,我们必须知道两点: 1.Spring 文件上传是基于common-fileUpload 组件的,所以,文件上传必须引入此包 2.Spring 文件上传需要在X ...

  4. Spring MVC必须知道的执行流程

    Spring MVC的执行流程 一.名词解释 1.前端控制器(DispatcherServlet) 接收请求,响应结果,相当于转发器,中央处理器 2.处理器映射器(HandlerMapping) 根据 ...

  5. Struts 2 Spring Hibernate三大框架的执行流程以及原理

    Struts2框架 一.简介 Struts2是一个相当强大的Java Web开源框架,是一个基于POJO的Action的MVC Web框架.它基于当年的WebWork和XWork框架,继承其优点,同时 ...

  6. Spring MVC 原理介绍(执行流程)

    Spring MVC工作流程图   图一   图二    Spring工作流程描述       1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServle ...

  7. ASP.NET MVC3 中整合 NHibernate3.3、Spring.NET2.0 使用AOP执行事务处理

    方法1 <object id="ServiceOperation" type="Spring.Aop.Support.SdkRegularExpressionMet ...

  8. Spring Batch在大型企业中的最佳实践

    在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...

  9. Spring Batch实践

    Spring Batch在大型企业中的最佳实践 在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后 ...

随机推荐

  1. springboot入门之版本依赖和自动配置原理

    前言 Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that ...

  2. NOIP模拟92&93(多校26&27)

    前言 由于太菜了,多校26 只改出来了 T1 ,于是直接并在一起写啦~~~. T0 NOIP 2018 解题思路 第一次考场上面写三分,然而我并不知道三分无法处理不是严格单峰的情况,但凡有一个平台都不 ...

  3. Oracle中对数字加汉字的排序

    需求:有一列NAME, varchar2类型,内容如下 以上就是已经按order by name进行排序的,但不是我们想要的结果 现在需要只按数字进行排序 第一步:抽取数字由于数字有是一位的有是两位的 ...

  4. Ubuntu安装BCC

    Ubuntu安装BCC 教程 官方文档 安装 这里官方文档中首先讲到的是二进制文件的安装,直接通过apt进行安装 sudo apt-get install bpfcc-tools linux-head ...

  5. 18.Java 封装详解/多态详解/类对象转型详解

    封装概述 简述 封装是面向对象的三大特征之一. 封装优点 提高代码的安全性. 提高代码的复用性. "高内聚":封装细节,便于修改内部代码,提高可维护性. "低耦合&quo ...

  6. oracle中是否有except函数: select * from a except select * from b

    除外: oracle没有except这个关键字,这个关键字属于sqlserver ,在oracle里,他叫minus,与except是一个意思. select a.code OrgId, b.code ...

  7. python实现对象测量

    目录: 问题,轮廓找到了,如何去计算对象的弧长与面积(闭合),多边形拟合,几何矩的计算等 (一)对象的弧长与面积 (二)多边形拟合 (三)几何矩的计算 (四)获取图像的外接矩形boundingRect ...

  8. [loj2977]巧克力

    先考虑第一个问题,即求最小的巧克力块数 将这张网格图建图(仅对$c_{i,j}\ne -1$的位置建点),即求点数最少的连通块(的点数)使得存在$k$个不同的$c_{i,j}$ (以下$c$仅用一维数 ...

  9. [atAGC034E]Complete Compress

    先考虑枚举最后的点,并以其为根 首先,操作祖先-后代关系是没有意义的,因为以后必然有一次操作会操作祖先使其返回原来的位置,那么必然不如操作后代和那一个点(少一次操作) 考虑某一次操作,总深度和恰好减2 ...

  10. gantt甘特图可拖拽、编辑(vue、react都可用 highcharts)

    前言   Excel功能强大,应用广泛.随着web应用的兴起和完善,用户的要求也越来越高.很多Excel的功能都搬到了sass里面.恨不得给他们做个Excel出来...程序员太难了... 去年我遇到了 ...