前面的例子step都是线性的流程,这里我们提供一个非线性的流程,也就是根据不同的状态做不同的流程step处理。万一有天悔恨变得太现实太世故太麻木,说不定能从回忆中重拾随兴飞翔。

step非线性的流程

  A step execution listener can change the exit status of a step. The job can then use the exit status for the transition decision to the next step。我们通过job传递参数来模拟不同的退出状态,从而来验证和加强非线性流程的学习。

一、在batch.xml中定义一个job

  1. <!--一个非线性流程的job-->
  2. <job id="noLinerJob">
  3. <step id="firstStep">
  4. <tasklet transaction-manager="transactionManager" ref="firstStepTasklet">
  5. <listeners>
  6. <listener ref="firstStepListener"/>
  7. </listeners>
  8. </tasklet>
  9. <next on="COMPLETED" to="completedStep"/>
  10. <next on="OWN STATUS" to="ownStatusStep"/>
  11. <next on="*" to="allStatusStep"/>
  12. </step>
  13. <step id="completedStep">
  14. <tasklet ref="completedTasklet"/>
  15. </step>
  16. <step id="ownStatusStep">
  17. <tasklet ref="ownStatusTasklet"/>
  18. </step>
  19. <step id="allStatusStep">
  20. <tasklet ref="allStatusTasklet"/>
  21. </step>
  22. </job>

  在step层面上的监听器中会有不同的返回状态,根据不同的状态我们做不同的流程处理。比如如果是COMPLETED,我们就执行completedStep。如果是OWN STATUS(我们自定义的变量),就执行ownStatusStep。如果上述变量都没有,那么我们执行正则匹配所有的allStatusStep。下面我们列出firstStepListener的定义与实现。

  1. <bean id="firstStepListener" class="spring.batch.noLiner.FirstStepListener" scope="step">
  2. <property name="status" value="#{jobParameters['status']}"/>
  3. </bean>

注意上述bean定义中的scope="step"是必须的,否则会报错。它的实现类代码如下

  1. package spring.batch.noLiner;
  2.  
  3. import org.springframework.batch.core.ExitStatus;
  4. import org.springframework.batch.core.StepExecution;
  5. import org.springframework.batch.core.listener.StepExecutionListenerSupport;
  6.  
  7. /**
  8. * @Author: huhx
  9. * @Date: 2017-11-02 下午 7:35
  10. */
  11. public class FirstStepListener extends StepExecutionListenerSupport {
  12. private String status;
  13.  
  14. public void setStatus(String status) {
  15. this.status = status;
  16. }
  17.  
  18. @Override
  19. public ExitStatus afterStep(StepExecution stepExecution) {
  20. if (status.equals("1")) {
  21. return new ExitStatus("OWN STATUS");
  22. } else if (status.equals("2")) {
  23. return new ExitStatus("NO STATUS");
  24. }
  25. return ExitStatus.COMPLETED;
  26. }
  27. }

至于ownStatusTasklet和completedTasklet以及allStatusTasklet的实现比较简单,就是打印一句话。这里就不再列举。以下是打印的结果。

  1. // 当sttus= != '1' && sttus= != '2'
  2. first step tasklet.
  3. completed status.
  4.  
  5. // 当sttus='1'
  6. first step tasklet.
  7. own status.
  8.  
  9. // 当sttus='2'
  10. first step tasklet.
  11. all status.

关于batch status与 exit status的区别:

  1. Spring Batch uses two concepts to represent the status of an execution: the batch sta- tus and the exit status. Both step execution and job execution have their own batch and exit statuses property. The batch status describes the status of the execution of a job or a step. The exit status represents the status of the job/step once the execution is finished.

二、关于上述的next,springbatch还提供了fail、stop与end

fail、stop与end的用法如下所示

  1. <step id="firstStep">
  2. <tasklet transaction-manager="transactionManager" ref="firstStepTasklet">
  3. <listeners>
  4. <listener ref="firstStepListener"/>
  5. </listeners>
  6. </tasklet>
  7. <next on="COMPLETED" to="completedStep"/>
  8. <end on="OWN STATUS"/>
  9. <fail on="*"/>
  10. </step>

它们的说明如下

友情链接

springbatch---->springbatch的使用(六)的更多相关文章

  1. YII内置验证规则

    required: 必填字段验证, 来自 CRequiredValidator类的别名 array(‘字段名列表用逗号隔开’, ‘required’),    就这样的一个小小的写法,可以让字段前面加 ...

  2. Spring Batch介绍

    简介 SpringBatch 是一个大数据量的并行处理框架.通常用于数据的离线迁移,和数据处理,⽀持事务.并发.流程.监控.纵向和横向扩展,提供统⼀的接⼝管理和任务管理;SpringBatch是Spr ...

  3. SpringBoot整合SpringBatch

    一.引入依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...

  4. 业务可视化-让你的流程图"Run"起来(6.定时任务&Spring-Batch的集成)

    前言 首先,感谢大家对上一篇文章[业务可视化-让你的流程图"Run"起来(5.SpringBoot集成&微服务编排)]的支持. 分享一下近期我对这个项目的一些改进. 在项目 ...

  5. springbatch操作CSV文件

    一.需求分析 使用Spring Batch对CSV文件进行读写操作: 读取一个含有四个字段的CSV文件(id, name, age, score), 对文件做简单的处理, 然后输出到还有一个csv文件 ...

  6. SpringBatch的核心组件JobLauncher和JobRepository

    Spring Batch的框架包括启动批处理作业的组件和存储Job执行产生的元数据.因此只需掌握配置这个基础框架在批处理应用程序中即启动Jobs并存储Job元数据. 组件:Job Launcher和J ...

  7. SpringBatch简介

    spring Batch是一个轻量级的.完善的批处理框架,旨在帮助企业建立健壮.高效的批处理应用.SpringBatch是Spring的一个子项目,使用Java语言并基于Spring框架为基础开发,使 ...

  8. spring-boot-oracle spring-batch

    Install/Configure Oracle express Oracle xe installer for linux (I don't care if you're running linux ...

  9. springbatch的封装与使用

    springbatch 主要实现批量数据的处理,我对batch进行的封装,提出了jobBase类型,具体job需要实现它即可.Spring Batch 不仅提供了统一的读写接口.丰富的任务处理方式.灵 ...

  10. SpringBatch的流程简介

    SpringBatch的流程图如下: 每个Batch都会包含一个Job.Job就像一个容器,这个容器装了若干Step,Batch中实际干活的也就是这些Step,至于Step干什么活,无外乎读取数据,处 ...

随机推荐

  1. 【未通过】LintCode #366 斐波纳契数列

    实现: public class Solution { /** * @param n: an integer * @return: an ineger f(n) */ public int fibon ...

  2. EXP 导出出错解决方案

    前言: 今天想要把 当前用户下的数据库 导出来,使用命令 导出数据库可用语句: exp bpmp/bkc123@127.0.0.1:5050/bkcyunty file=D:\bak\db_61.dm ...

  3. CI框架伪静态化配置

    CI框架伪静态化配置 伪静态化,即:去掉入口的index.php, 在url后面加上 .html 后缀 CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/ ...

  4. JAVA的get post 区别

    1. get 是从服务器上获取数据,post 是向服务器传送数据. get 请求返回 request - URI 所指出的任意信息.Post 请求用来发送电子邮件.新闻或发送能由交互用户填写的表格.这 ...

  5. springmvc接口ios网络请求

    springmvc:   application/json;charset=utf-8的ios网络请求: 后台使用 @RequestBody注解参数接收:

  6. koa项目用mongoose与mongodb交互,始终报错FormModel is not defined

    koa项目用mongoose与mongodb交互,始终报错FormModel is not defined,就是自己定义的model实例始终不能找到,但是明明定义了,这时候就要看大小写了,当创建一个m ...

  7. python06 深浅拷贝原理

    preface 这里主要说深浅拷贝的原理.首先说说数字与字符串作为内存对象的重用,请看代码: a1=12345 b1=12345 a2=a1 print(id(a1),id(b1),id(a2)) 打 ...

  8. False 'Sharing Violation' Xcopy error message

    今天想要将QC的新工具自动拷贝到p4 用户机器上使用,为了避免每次通知大家升级啊!!! 于是,我在程序里调用了bat文件,执行拷贝操作,想在默默的情况下替换更新新版本工具,结果我测试发现没能成功更新版 ...

  9. Python的Beautiful Soup简单使用

    Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据 Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功能 它是一个工具箱, ...

  10. get calllog fail

    coolpad Coolpad 8122   Uri smsUri = CallLog.Calls.CONTENT_URI;     Cursor callLogCursor = cr.query(s ...