kgb
kgb

Reputation: 79

How to define parallel sub-flow in another flow in spring batch?

I want to implement the flow structure as below in spring batch.

           Job
          /   \
       Flow1  Flow2  
         /      \
      Step1    Step2
        /       /  \
       /    Step3  Flow3
      /                \  
     /                 Step4
     \                  /
      \                /
       \              /    
            Step5    

The job configuration pseudo code is as below:

@Configuration
public class JobConfiguration {

......

    @Bean
    public Job Job() {

    Flow flow1 = new FlowBuilder<SimpleFlow>("flow1")
                            .start(step1())
                            .build();

    Flow flow2 = new FlowBuilder<SimpleFlow>("flow2")
           .start(step2())
           .next(step3())
           .split(new SimpleAsyncTaskExecutor()).add(flow3)
           .build();

    Flow flow3 = new FlowBuilder<SimpleFlow>("flow3")
                            .start(step4())
                            .build();

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

When I run the batch, the log shows that step1, step2, step3 and step5 are executed, but step4 is not run.

I am wondering how to define sub-flow inside another flow, is the above code the right way to implement it?

Thanks in advance!

Upvotes: 0

Views: 351

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31745

Running each flow in isolation shows that Flow1 and Flow3 are correct, but Flow2 isn't. Running only Flow2 with:

    return jobBuilderFactory.get("job")
            .incrementer(new RunIdIncrementer())
            .start(flow2)
            .build()
            .build();

shows that step2 and step3 are executed, but not step4. So the problem is with the definition of this flow.

You need to define a parallel flow between Step3 and Flow3 as you did for Flow1 and Flow2. Here is an example:

@Bean
public Job Job() {

    Flow flow1 = new FlowBuilder<SimpleFlow>("flow1")
            .start(step1())
            .build();

    Flow flow3 = new FlowBuilder<SimpleFlow>("flow3")
            .start(step4())
            .build();

    Flow parallelFlow = new FlowBuilder<SimpleFlow>("parallelFlow")
            .start(step3())
            .split(new SimpleAsyncTaskExecutor()).add(flow3)
            .build();

    Flow flow2 = new FlowBuilder<SimpleFlow>("flow2")
            .start(step2())
            .next(parallelFlow)
            .build();

    return jobs.get("job")
            .incrementer(new RunIdIncrementer())
            .start(flow1)
            .split(new SimpleAsyncTaskExecutor()).add(flow2)
            .next(step5())
            .end()
            .build();
}

Hope this helps.

Upvotes: 0

Related Questions