Spring 使用 java 配置的批量流声明

Spring batch flow declaration using java config

我正在阅读 spring 批处理文档并停留在 following part:

提供了以下示例:

@Bean
public Job job() {
        Flow flow1 = new FlowBuilder<SimpleFlow>("flow1")
                        .start(step1())
                        .next(step2())
                        .build();
        Flow flow2 = new FlowBuilder<SimpleFlow>("flow2")
                        .start(step3())
                        .build();

        return this.jobBuilderFactory.get("job")
                                .start(flow1)
                                .split(new SimpleAsyncTaskExecutor())
                                .add(flow2)
                                .next(step4())
                                .end()
                                .build();
}

但是没有解释发生了什么。

据我所知,flow1 和 flow2 是并行执行的,但是 step4 呢?

step4()flow1flow2返回后线性执行。

查看 FlowBuilder.SplitBuilder.add() javadoc:

public FlowBuilder<Q> add(Flow... flows)

Add flows to the split, in addition to the current state already present in the parent builder.

Parameters:

flows - more flows to add to the split

Returns: the parent builder

它 returns 父构建器而不是当前 SplitBuilder 对象。
因此它不包含在流拆分中,因此按顺序执行。

到 运行 3 个并行流:

 return this.jobBuilderFactory.get("job")
                              .start(flow1)
                              .split(new SimpleAsyncTaskExecutor())
                              .add(flow2, step4())                   
                              .end()
                              .build();