Spring batch flow declaration using java config

170 Views Asked by At

I am reading the spring batch documentation and stuck on following part:

There are provided following example:

@Bean
public Job job() {
        Flow flow1 = new FlowBuilder<SimpleFlow>("flow1")
                        .start(step1())
                        .next(step2())
                        .build();
        Flow flow2 = new FlowBuilder<SimpleFlow>("flow2")
                        .start(step3())
                        .build();

        return this.jobBuilderFactory.get("job")
                                .start(flow1)
                                .split(new SimpleAsyncTaskExecutor())
                                .add(flow2)
                                .next(step4())
                                .end()
                                .build();
}

But it is not explained what is happening.

as far I understand flow1 and flow2 are executed in parallel but what about step4 ?

1

There are 1 best solutions below

0
davidxxx On BEST ANSWER

step4() is executed linearly after flow1 and flow2 returned.

Look at the FlowBuilder.SplitBuilder.add() javadoc :

public FlowBuilder<Q> add(Flow... flows)

Add flows to the split, in addition to the current state already present in the parent builder.

Parameters:

flows - more flows to add to the split

Returns: the parent builder

It returns the parent builder and not the current SplitBuilder object.
So it is not included in the flow split and so is executed sequentially.

To run the 3 flows in parallel :

 return this.jobBuilderFactory.get("job")
                              .start(flow1)
                              .split(new SimpleAsyncTaskExecutor())
                              .add(flow2, step4())                   
                              .end()
                              .build();