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

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.

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.

Details:

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

6. Create a Job Launch

Use a Controller for launch a Batch Job

7. Enable Batch Job

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

Detail:

8. Run & Check Result

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

Make a launch request:
http://localhost:8080/launchjob

Logs

IV. Source code

SpringBatchParallelStep


Related Posts


2 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@javasampleapproach.com

      Regards,
      JSA

Got Something To Say:

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

*