Spring Batch 3.0.2使用注释的并行步骤

时间:2015-02-09 21:42:34

标签: spring-batch

我正在使用SpringBatch 3.0.2并尝试创建一个使用带注释的JavaConfig样式和JobBuilderFactory类执行一系列步骤(其中一些并行)的作业。我想要发生的是从step1开始,当它完成转换到step2a,step2b,step2c和step2d时,它们都是并行运行的。当所有这些步骤完成后,它应该转换到步骤3。我正在努力创建这项工作所需的语法

@Bean(name = "SplitFlowJob4")
 public Job splitFlowJob4() throws SQLException
  {
    final Flow flow2b = new FlowBuilder<Flow>("subflow2b").from(step2b()).end();
    final Flow flow2c = new FlowBuilder<Flow>("subflow2c").from(step2c()).end();
    final Flow flow2d = new FlowBuilder<Flow>("subflow2d").from(step2d()).end();

    return jobs.get("SplitFlowJob4").start(step1()).next(step2a()).split(new SimpleAsyncTaskExecutor())
        .add(flow2b, flow2c, flow2d).next(step3()).end().build();
  }

这不符合我的预期。这仅执行step1和step2a。我没有看到step2b,2c,2d或step3。

    [INFO ] [main] 2015-02-09 15:28:32,169 org.springframework.batch.core.job.SimpleStepHandler - Executing step: [SplitFlowJobStep1]
    [INFO ] [main] 2015-02-09 15:28:32,194 org.springframework.batch.core.job.SimpleStepHandler - Executing step: [SplitFlowJobStep2a]

如果我改变语法并从step1调用split,则step1,2a,2b,2c,2d全部并行执行,而不仅仅是步骤2a,2b,2c,2d。

  @Bean(name = "SplitFlowJob5")
  public Job splitFlowJob5() throws SQLException
  {
    final Flow flow2a = new FlowBuilder<Flow>("subflow2a").from(step2a()).end();
    final Flow flow2b = new FlowBuilder<Flow>("subflow2b").from(step2b()).end();
    final Flow flow2c = new FlowBuilder<Flow>("subflow2c").from(step2c()).end();
    final Flow flow2d = new FlowBuilder<Flow>("subflow2d").from(step2d()).end();

    return jobs.get("SplitFlowJob5").start(step1()).split(new SimpleAsyncTaskExecutor())
        .add(flow2a, flow2b, flow2c, flow2d).next(step3()).end().build();
  }

这将并行执行Step1,step2a,step2b,step2c,step2d。

    [INFO ] [SimpleAsyncTaskExecutor-2] 2015-02-09 16:28:36,507 org.springframework.batch.core.job.SimpleStepHandler - Executing step: [SplitFlowJobStep2b]
    [INFO ] [SimpleAsyncTaskExecutor-5] 2015-02-09 16:28:36,513 org.springframework.batch.core.job.SimpleStepHandler - Executing step: [SplitFlowJobStep1]
    [INFO ] [SimpleAsyncTaskExecutor-3] 2015-02-09 16:28:36,512 org.springframework.batch.core.job.SimpleStepHandler - Executing step: [SplitFlowJobStep2c]
    [INFO ] [SimpleAsyncTaskExecutor-4] 2015-02-09 16:28:36,509 org.springframework.batch.core.job.SimpleStepHandler - Executing step: [SplitFlowJobStep2d]
    [INFO ] [SimpleAsyncTaskExecutor-1] 2015-02-09 16:28:36,507 org.springframework.batch.core.job.SimpleStepHandler - Executing step: [SplitFlowJobStep2a]
    [INFO ] [main] 2015-02-09 16:28:36,615 org.springframework.batch.core.job.SimpleStepHandler - Executing step: [SplitFlowJobStep3]

我不确定我做错了什么,是否可以使用流利的构建器api创建这样的工作?我已经看到其他工作示例以分裂开始或结束,但我还没有看到在工作中间包含分裂的那些。

1 个答案:

答案 0 :(得分:4)

Spring Batch的java配置中存在一个错误,其中包含上周晚些时候修复的拆分(BATCH-2346)。修复程序解决了阻止执行列表中第一个流程的错误以及如何解决此用例的文档修复。

简而言之,以下内容应与最新代码一起使用:

@Bean(name = "SplitFlowJob5")
public Job splitFlowJob5() throws SQLException
{
    final Flow flow2a = new FlowBuilder<Flow>("subflow2a").from(step2a()).end();
    final Flow flow2b = new FlowBuilder<Flow>("subflow2b").from(step2b()).end();
    final Flow flow2c = new FlowBuilder<Flow>("subflow2c").from(step2c()).end();
    final Flow flow2d = new FlowBuilder<Flow>("subflow2d").from(step2d()).end();

    final Flow splitFlow = new FlowBuilder<Flow>("splitFlow")
        .start(flow2a)
        .split(new SimpleAsyncTaskExecutor())
        .add(flow2b, flow2c, flow2d).build();

    return jobs.get("SplitFlowJob5").start(step1())
       .next(splitFlow).next(step3()).end().build();
}