Spring Batch Job with Parallel Steps

Spring Batch Job with Parallel Steps

For scaling a Batch Job, Parallel Steps is one solution that bases on the business logic of application. We split the logic business in distinct responsibilities, and each step can be executed in parallelized flow. The tutorial will guide you how to configure parallel steps with Spring Batch.

Related Articles:
Spring Batch Partition for Scaling & Parallel Processing
How to start with Spring Batch using Spring Boot – Java Config

I. Technologies for spring batch parallel step tutorial

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot
– MySQL Database

II. Overview

1. Structure of project

spring batch parallel step structure-of-project

2. Step to do

– Create Spring Boot project
– Add dependencies
– Config Batch Job DataSource
– Create a Simple Tasklet Step
– Create a Job Launch
– Enable Batch Job
– Define Spring Batch Job with XML config
– Run & Check Result

III. Practices

1. Create Spring Boot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info. Then press Finish, spring boot project will be created.

2. Add dependencies

Add spring-boot-starter-batch, spring-boot-starter-web, mysql-connector-java

<?xml version="1.0" encoding="UTF-8"?>


3. Config Batch Job DataSource

Open application.properties, config spring-datasource for batch job repository


4. Create a Simple Tasklet Step

Create a SimpleStep that implements the Tasklet interface.
We define a workload function for simulating the hard processing.

package com.javasampleapproach.batch.parallelstep.step;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

public class SimpleStep implements Tasklet{

	public RepeatStatus execute(StepContribution contribution,
			ChunkContext chunkContext) throws Exception {
		return RepeatStatus.FINISHED;
	private void workload() throws Exception{


5. Define Spring Batch Job with XML config

Here is the main part of tutorial.

spring batch parallel step-flow-split

We defines 4 steps: step_1, step_2, step_3, step_4. Step_1, Step_2, Step_3 are designed for parallel processing with 3 flow in a split split_1.

After split_1 is Done, it means: step_1, step_2, step_3 are Done, then step_4 is processed.


<beans:beans xmlns="http://www.springframework.org/schema/batch"
	xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	<job id="job">
		<split id="split_1" task-executor="taskExecutor" next="step_4">
				<step id="step_1">
					<tasklet ref="taskletStep_1"/>
				<step id="step_2">
					<tasklet ref="taskletStep_2"/>
				<step id="step_3">
					<tasklet ref="taskletStep_3"/>
		<step id="step_4">
					<tasklet ref="taskletStep_3"/>

	<beans:bean id="taskletStep_1" class="com.javasampleapproach.batch.parallelstep.step.SimpleStep" />
	<beans:bean id="taskletStep_2" class="com.javasampleapproach.batch.parallelstep.step.SimpleStep" />
	<beans:bean id="taskletStep_3" class="com.javasampleapproach.batch.parallelstep.step.SimpleStep" />
	<beans:bean id="taskletStep_4" class="com.javasampleapproach.batch.parallelstep.step.SimpleStep" />
	<beans:bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />


The TaskExecutor is defaulted by a SyncTaskExecutor, but for parallel processing batch job requires an asynchronous TaskExecutor. So we set task-executor=”taskExecutor” with:

<beans:bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />

6. Create a Job Launch

Use a Controller for launch a Batch Job

package com.javasampleapproach.batch.parallelstep.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class JobLauncherController {

	JobLauncher jobLauncher;

	Job job;

	public String handle() throws Exception {

		Logger logger = LoggerFactory.getLogger(this.getClass());
		try {
			JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
			jobLauncher.run(job, jobParameters);
		} catch (Exception e) {

		return "Done";

7. Enable Batch Job

In the main class of Spring boot, annotation with @EnableBatchProcessing


package com.javasampleapproach.batch.parallelstep;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

public class SpringBatchParallelStepApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBatchParallelStepApplication.class, args);

8. Run & Check Result

Build project with Maven: clean install
Run project with mode: Spring Boot App

Make a launch request:


2016-10-13 09:27:00.868  INFO 3140 --- [cTaskExecutor-2] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step_2]
2016-10-13 09:27:00.883  INFO 3140 --- [cTaskExecutor-1] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step_1]
2016-10-13 09:27:00.883  INFO 3140 --- [cTaskExecutor-3] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step_3]
2016-10-13 09:27:06.015  INFO 3140 --- [nio-8080-exec-1] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step_4]
2016-10-13 09:27:11.059  INFO 3140 --- [nio-8080-exec-1] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=job]] completed with the following parameters: [{time=1476325617042}] and the following status: [COMPLETED]

IV. Source code


16 thoughts on “Spring Batch Job with Parallel Steps”

  1. i really like this tutorial,i implemented one spring batch application as POC my client like that and we deployed that in production,i implemented that application which use only one thread.
    but we added more client and now getting huge amount of request,we deployed the application in PCF,by using PCF i created 2 instance of my spring batch application, but the problem i am getting is, both instances are getting same data from the data base while reading it.
    i am using jdbcCursorItemReader, can you please suggest a way how can i prevent duplicate reads from instances.client want to use horizontal scaling,no partition and threads.

    1. Hi,

      For your system, I recommend you to re-design architecture. You should have a Queue implementation before your jobs. Then duplicated problem will be resolve
      and your system’s architecture will be clearer and stronger.

      About Queue implementation, you can choose: ActiveMQ, or RabitMQ, or Kafka.

      – For starting with ActiveMQ, you can refer:
      How to use Spring JMS with ActiveMQ – JMS Consumer and JMS Producer | Spring Boot

      – For starting with RabitMQ, you can refer:
      RabbitMq – How to create Spring RabbitMq Publish/Subcribe pattern with SpringBoot

      RabbitMQ – How to create Spring RabbitMQ Producer/Consumer applications with SpringBoot

      – For starting with Kafka, you can refer:
      How to start Spring Apache Kafka Application with SpringBoot Auto-Configuration

      If you want to know more about architecture, We can discuss more via email: contact@ozenero.com


  2. We absolutely love your blog and find most of your post’s to be just what I’m looking for. Would you offer guest writers to write content to suit your needs? I wouldn’t mind producing a post or elaborating on most of the subjects you write concerning here. Again, awesome blog!

  3. Aw, this was an extremely nice post. Spending some time and actual effort to produce a top notch article… but what can I say… I hesitate
    a lot and don’t seem to get anything done.

  4. I’m not positive the place you’re getting your information,
    but good topic. I needs to spend a while studying more or understanding more.
    Thanks for magnificent info I used to be searching for this info for my mission.

  5. Hello, Neat post. There is an issue together with your website in internet
    explorer, could test this? IE still is the marketplace chief
    and a big component to folks will miss your wonderful writing
    because of this problem.

  6. you are really a just right webmaster. The site loading velocity
    is amazing. It seems that you’re doing any distinctive
    trick. Moreover, The contents are masterwork. you’ve performed a magnificent
    activity in this matter!

  7. Hody arеe using WorԀpresѕ for your sіte
    platform? I’m new to tһe blog world bbut I’m trying to get starteɗ and set up my own. Do you need any html coding
    еxpertiѕe to make yоur oown blog? Any help would be really ɑppreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *