Spring Batch:java配置文件中的并行步骤执行

时间:2016-05-30 07:41:41

标签: spring-batch

我正在尝试在java配置文件中进行并行步执行的示例应用程序,但是感到困惑的是,正在配置和初始化了多少文件(作业存储库,作业启动器和执行等),如果已配置,那么如何? 我只需要一个示例应用程序来阐明并行执行作业中步骤的基础知识。

3 个答案:

答案 0 :(得分:7)

这是通过java配置使用拆分的示例。在此示例中,流程1和2将并行执行:

@Configuration
public class BatchConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Tasklet tasklet() {
        return new CountingTasklet();
    }

    @Bean
    public Flow flow1() {
        return new FlowBuilder<Flow>("flow1")
                .start(stepBuilderFactory.get("step1")
                        .tasklet(tasklet()).build())
                .build();
    }

    @Bean
    public Flow flow2() {
        return new FlowBuilder<Flow>("flow2")
                .start(stepBuilderFactory.get("step2")
                        .tasklet(tasklet()).build())
                .next(stepBuilderFactory.get("step3")
                        .tasklet(tasklet()).build())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("job")
                .start(flow1())
                .split(new SimpleAsyncTaskExecutor()).add(flow2())
                .end()
                .build();
    }

    public static class CountingTasklet implements Tasklet {

        @Override
        public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
            System.out.println(String.format("%s has been executed on thread %s", chunkContext.getStepContext().getStepName(), Thread.currentThread().getName()));
            return RepeatStatus.FINISHED;
        }
    }
}

答案 1 :(得分:5)

假设您有步骤A,B1,B2,B3,C。你想要运行B1,B2&amp; B3并行。首先需要为它们创建子流,然后使用SimpleAsyncTaskExecutor()添加到一个流:

@Bean
public Job job()
{
    final Flow flowB1 = new FlowBuilder<Flow>("subflowb1").from(stepb1()).end();
    final Flow flowB2 = new FlowBuilder<Flow>("subflowb2").from(stepb2()).end();
    final Flow flowB3 = new FlowBuilder<Flow>("subflowb3").from(stepb3()).end();

    final Flow splitFlow = new FlowBuilder<Flow>("splitFlow")
        .start(flowB1)
        .split(new SimpleAsyncTaskExecutor())
        .add(flowB2, flowB3).build();

    return jobBuilderFactory
       .flow(stepA())
       .next(splitFlow)
       .next(stepC())
       .end()
       .build();
}

答案 2 :(得分:-1)

这里是不同数据集的基本并行步骤执行,基本上你必须提供一个分区程序,它将为每个步骤创建单独的上下文,并根据你可以处理其数据集的上下文。

{{1}}