How to run spring batch jobs in parallel


I have a job flow and I would like to run it as the following:

Job1 -> Job2 -> Job3  
     -> Job4 -> Job5

The job flow will be started from Job1.
After Job1 is successfully finished, Job1 will launch both Job2 and Job4.
Job2 and Job4 will run in parallel.
After Job2 is successfully finished, Job2 will launch Job3.
After Job4 is successfully finished, Job4 will launch Job5.

The following is the code snippet for job1.xml and job launcher class of Job1:


<bean id="uiJobListener"
    <property name="vmInfoImportUIBatchLauncher" ref="vmInfoImportUIBatchLauncher" />
    <property name="jobRepository" ref="jobRepository" />

<bean id="uiBatchLauncher"
    <property name="simpleJobLauncher" ref="simpleJobLauncher" />
    <property name="jobLocator" ref="jobRegistry" />
    <property name="jobTwo" value="Job2" />
    <property name="jobFour" value="Job4" />

<batch:job id="Job1" restartable="true">
    <batch:step id="stp01">
        <batch:tasklet ref="stp01Operator" />
        <batch:next on="COMPLETED" to="stp02" />

    <batch:step id="stp02">
        <batch:tasklet ref="stp02Result" />

        <batch:listener ref="uiJobListener" />

Job jobOne = jobLocator.getJob(jobTwo);, builder.toJobParameters());

Job jobTwo = jobLocator.getJob(jobFour);, builder.toJobParameters());

The Problem

But, when I started Job1, Job1 launched Job2 and Job2 continued to Job3.
After Job3 is finished, Job1 launched Job4 and Job4 continued to Job5.

"Job2, Job3 " pair and "Job4, Job5" pair didn't run in parallel.
Although Job1 launched Job4, job flow became as the following:

Job1 -> Job2 -> Job3 -> Job4 -> Job5

So, how can the spring batch jobs be run in parallel?
Is there a way to run spring batch jobs in parallel both from Spring Batch Admin UI and Command Line?

Best Solution

Jobs can't naturally managed as you ask.
You have to create a new super job and using a 'Split flow' configuration redirect job as you prefer.
Of course split works for steps and not for jobs, but SB has the possibility to wrap a job into a step using a JobStep
Follow official doc and examples about split around the net and you should be able to solve your problem without problem

Related Question