Spring Batch Job with Parallel Steps
Tags: springbatch, parallel, springboot
Published: 2021-04-25 18:04:45

https://grokonez.com/spring-framework/spring-batch/spring-batch-job-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:
- <a href="https://grokonez.com/spring-framework/spring-boot/spring-batch-partition-scaling-parallel-processing">Spring Batch Partition for Scaling & Parallel Processing</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/start-spring-batch-using-spring-boot" target="_blank" rel="noopener noreferrer">How to start with Spring Batch using Spring Boot – Java Config</a>

<!--more-->

<iframe width="853" height="480" src="https://www.youtube.com/embed/LMYTulZ7wGc?rel=0" frameborder="0" allowfullscreen></iframe>
<div id="tuttoc" class="jsa-toc"></div>

<div id="tutcontents">
<h2>I. Technologies for spring batch parallel step tutorial</h2>
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot
- MySQL Database
<h2>II. Overview</h2>
<h3>1. Structure of project</h3>
<img class="alignnone size-full wp-image-1665" src="https://grokonez.com/wp-content/uploads/2016/10/spring-batch-parallel-step-structure-of-project.png" alt="spring batch parallel step structure-of-project" width="646" height="464" />
<h3>2. Step to do</h3>
- 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

<h2>III. Practices</h2>
<h3>1. Create Spring Boot project</h3>
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.

<h3>2. Add dependencies</h3>
Add <strong>spring-boot-starter-batch, spring-boot-starter-web, mysql-connector-java</strong>

<pre><code class="language-html">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;dependencies&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;/dependency&gt;
	
	&lt;dependency&gt;
	    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	    &lt;artifactId&gt;spring-boot-starter-batch&lt;/artifactId&gt;
	&lt;/dependency&gt;  
	
      &lt;dependency&gt;
           &lt;groupId&gt;mysql&lt;/groupId&gt;
           &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
           &lt;scope&gt;runtime&lt;/scope&gt;
       &lt;/dependency&gt;
	
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;                              
&lt;/dependencies&gt;</code></pre>

<h3>3. Config Batch Job DataSource</h3>
Open application.properties, config spring-datasource for batch job repository
<pre><code class="language-java">
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=12345
spring.batch.job.enabled=false
</code></pre>

https://grokonez.com/spring-framework/spring-batch/spring-batch-job-parallel-steps

Spring Batch Job with Parallel Steps