Configuring Spring Batch Steps in Parallel (Split) using Annotations

10,322

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);
Share:
10,322
Dhanush Gopinath
Author by

Dhanush Gopinath

I am CTO &amp; Co-founder at Geektrust, an online recruitment marketplace. I code in Go, Javascript and Python and used to work on Java before I started Geektrust. I have been programming for about 16 years now and love to explore new programming languages and technology.

Updated on June 04, 2022

Comments

  • Dhanush Gopinath
    Dhanush Gopinath almost 2 years

    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();
    }
    
  • Dhanush Gopinath
    Dhanush Gopinath over 9 years
    Thanks Michael. I shall check this tomorrow and update
  • Dhanush Gopinath
    Dhanush Gopinath over 9 years
    Yes it works. Thank you. Hope you add this to the Spring Documentation