混合顺序和并行步骤处理

Mixing sequential and parallel step processing

我扩展了 FlowJob class 并按如下方式设置流程:

final Flow subflow1 = new FlowBuilder<Flow>("subflow1")
    .start(new StepBuilder("SubFlow1-step")
            .chunk(1000)
            .reader(reader)
            .writer(writer)
            .repository(jobRepository)
            .transactionManager(txManager).build())
    .end();
final Flow subflow2 = new FlowBuilder<Flow>("subflow2")
    .start(new StepBuilder("SubFlow2-step")
            .chunk(1000)
            .reader(reader)
            .writer(writer)
            .taskExecutor(new SimpleAsyncTaskExecutor())
            .repository(jobRepository)
            .transactionManager(txManager).build())
    .end();

setFlow(new FlowBuilder<Flow>("Flow")
            .start(new StepBuilder("Flow-step1")
                    .chunk(10000)
                    .reader(reader)
                    .writer(writer)
                    .repository(jobRepository)
                    .transactionManager(txManager).build())
            .next(new StepBuilder("Flow-step2")
                    .tasklet(processor)
                    .repository(jobRepository)
                    .transactionManager(txManager).build())
            .split(new SimpleAsyncTaskExecutor())
            .add(subflow1, subflow2)
            .end());

如你所见,我想做的事情如下:

  1. 执行步骤 1
  2. 执行步骤 2
  3. 同时执行subflow1和subflow2。 subflow1 步骤将按顺序执行。 subflow2 步骤将并行执行(分派给可用线程的块)。

当我运行我的工作时,步骤 1 和 2 正确执行,然后工作终止。

有什么想法吗?

编辑: 关于 SO Spring-batch flow / split after a step 的相关问题。

我相信记录了一个关于此的错误。目前的解决方法是使用您的拆分创建一个流并将该流添加到作业中,如下所示:

@Bean
public Job splitJob(@Qualifier("foo") Flow foo, @Qualifier("foo") Flow bar) {
    FlowBuilder<Flow> flowBuilder = new FlowBuilder<>("split");

    Flow flow = flowBuilder.split(new SimpleAsyncTaskExecutor())
            .add(foo, bar)
            .end();

    return jobBuilderFactory.get("splitJob")
            .start(myStep1())
            .next(myStep2())
            .on("COMPLETED").to(flow)
            .end()
            .build();
}