【问题标题】:Configuring Spring Batch Steps in Parallel (Split) using Annotations使用注释并行(拆分)配置 Spring Batch 步骤
【发布时间】:2015-02-21 08:32:18
【问题描述】:

在我查看 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 配置配置拆分步骤?我检查了 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();
}

【问题讨论】:

    标签: java spring spring-boot spring-batch


    【解决方案1】:

    SimpleJobBuilder 提供了通过 java config 配置拆分的工具。下面是取自FlowJobBuilderTests (https://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);
    

    【讨论】:

    • 谢谢迈克尔。我明天会检查并更新
    • 是的,它有效。谢谢你。希望您将此添加到 Spring 文档中
    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2016-07-19
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多