Spring批处理分区有多个顺序运行?

时间:2018-11-29 21:04:55

标签: java spring spring-batch

我需要创建一个主步骤,并按顺序运行多个从步骤。我可以在主机中定义单个从机,但是我需要顺序运行从机。

@Primary
@Profile(MASTER)
@Bean("masterStep")
public Step partitionCreateStepForRemote(StepBuilderFactory stepBuilderFactory,
                            @Qualifier("slave1") Step step,
                            MatchAsyncConfig asyncConfig,
                            MatchingAccountPartitioner partitioner,
                            JMSPartitionHandler messageChannelPartitionHandler,
                            JobRepository jobRepository,
                            @Qualifier("stepLocator") StepLocator stepLocator
)                 {

 SimpleStepExecutionSplitter splitter = new SimpleStepExecutionSplitter();
splitter.setPartitioner(partitioner);
splitter.setJobRepository(jobRepository);

return stepBuilderFactory.get("masterStep")
        .partitioner(step)
        .partitionHandler(messageChannelPartitionHandler)
        .splitter(splitter)
        .taskExecutor(asyncConfig.getAsyncExecutor())
        .build();
}

是否可以在同一个分区程序下的单个主步骤中定义多个从步骤,如下所示?

public Step partitionCreateStepForRemote(StepBuilderFactory stepBuilderFactory,
                            @Qualifier("slave1") Step step,
                            @Qualifier("slave2") Step step,
                            @Qualifier("slave3") Step step, 
                            MatchAsyncConfig asyncConfig,
                            MatchingAccountPartitioner partitioner,
                            JMSPartitionHandler messageChannelPartitionHandler,
                            JobRepository jobRepository,
                            @Qualifier("stepLocator") StepLocator stepLocator
)                 {}

enter image description here

1 个答案:

答案 0 :(得分:0)

我只需要在从属步骤内部定义Flow。该流程将按顺序进行。 从属步骤不过是在内部定义所有子步骤而已。因此,所有从属步骤都并行运行,每个从属都按顺序运行子级从属步骤。

/**
 * @param stepBuilderFactory   step for run each partition
 * @return Step step 6 partition match
 */
@Profile({SINGLE, SLAVE})  //Not applicable for MASTER profile in remote partitioning case
@Bean("slaveStep")
public Step slaveStep(StepBuilderFactory stepBuilderFactory,
                               @Qualifier("readFromFileFlow") Flow flow
) {
    return stepBuilderFactory.get("slaveStep")
            .listener(stepListener())
            .flow(flow)
            .build();
}

@Profile({SINGLE, SLAVE})
@Bean
public ChildSlaveListener stepListener(){
    return new ChildSlaveListener();
}

@Profile({SINGLE, SLAVE})
@Bean("readFromFileFlow")
public Flow readFromFileFlow(@Qualifier("childSlaveStep1") Step childSlaveStep1,
                             @Qualifier("childSlaveStep2") Step childSlaveStep2) {
    // @formatter:off
    return new FlowBuilder<Flow>("readFromFileFlow")
            .start(childSlaveStep1)
            .on(FAILED).fail()
            .from(childSlaveStep1)
            .on(COMPLETED).to(childSlaveStep2)
            .start(childSlaveStep2)
            .on(FAILED).fail()
            .from(childSlaveStep2)
            .on(COMPLETED).end()
            .end();
    // @formatter:on
}