gpt4 book ai didi

java - 使用注释并行(拆分)配置 Spring Batch 步骤

转载 作者:行者123 更新时间:2023-12-02 03:57:09 30 4
gpt4 key购买 nike

每当我查看 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,并且所有配置都是使用注释完成的。我可以使用 Java 配置来配置分割步骤吗?我检查了API documentation Spring Batch 中的 Step 接口(interface),但它没有 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();
}

最佳答案

SimpleJobBuilder 提供了通过 java 配置来配置拆分的工具。以下是取自 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);

关于java - 使用注释并行(拆分)配置 Spring Batch 步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27616300/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com