使用注释配置并行(拆分)的Spring批处理步骤

时间:2014-12-23 08:01:26

标签: java spring spring-boot spring-batch

无论我在哪里查看Spring Batch文档并行执行步骤,我只能通过XML看到它的配置,如下所示。

<split id="split1" next="step4">
<flow>
    <step id="step1" parent="s1" next="step2"/>
    <step id="step2" parent="s2"/>
</flow>
<flow>
    <step id="step3" parent="s3"/>
</flow>

我正在使用Spring Batch编写应用程序,我也使用了Spring Boot,并且所有配置都是使用Annotations完成的。有没有我可以使用Java配置配置Split Step?我检查了Spring Batch中Step接口的API documentation,但它没有Split Step的默认实现。有没有办法可以使用现有的默认实现来实现它?

目前我已经实现了我的其他工作:

@Bean
public Step someStep() {
    return stepBuilderFactory.get("someStep")
            .<A, B> chunk(1-).reader(someReader)
            .processor(someProcessor).writer(someWriter).build();
}

@Bean
public Job historicalDataJob() {
    return jobBuilderFactory.get("someJOb")
            .incrementer(new RunIdIncrementer()).flow(someStep()).end()
            .build();
}

1 个答案:

答案 0 :(得分:10)

SimpleJobBuilder提供了通过java配置配置拆分的功能。以下是取自FlowJobBuilderTestshttps://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/test/java/org/springframework/batch/core/job/builder/FlowJobBuilderTests.java)的示例。显然你会想要解决这个问题,但它应该说明一般的想法。

// Create each flow
Flow flow = new FlowBuilder<Flow>("subflow").from(step1).end();

// Create the job
SimpleJobBuilder builder = new JobBuilder("flow").repository(jobRepository).start(step2);

// Create split providing an async task executor so the flows are executed in parallel
builder.split(new SimpleAsyncTaskExecutor()).add(flow).end();

// Build the job and execute it
builder.preventRestart().build().execute(execution);