Configuring Spring Batch Steps in Parallel (Split) using Annotations

孤人 提交于 2020-01-13 19:21:06

问题


Where ever I look into Spring Batch documentation for executing steps in parallel, I only see the configuration of it via XML like given below.

<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>

I am writing an application using Spring Batch where I have used Spring Boot as well, and all of my configurations are done using Annotations. Is there a I can configure a Split Step using Java configuration? I checked the API documentation of Step interface in Spring Batch, but it doesn't have a default implementation for Split Step. Is there way I can implement it using the existing default implementations?

Currently I have implemented my other jobs like this :

@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:


The SimpleJobBuilder provides facilities for configuring a split via java config. Below is an example taken from the FlowJobBuilderTests (https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/test/java/org/springframework/batch/core/job/builder/FlowJobBuilderTests.java). Obviously you'll want to break this up a bit, but it should illustrate the general idea.

// 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);


来源:https://stackoverflow.com/questions/27616300/configuring-spring-batch-steps-in-parallel-split-using-annotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!