How to start with Spring Batch using Spring Boot – Java Config
Many business operations need to process with batch job for critical environment. Spring Batch is a lightweight framework to boot the batch application.
The tutorial will guide you how to start with Spring Batch using Spring Boot.
Related Artical: How to start with Spring Batch using Spring Boot – XML Config
I. Technologies for Spring Batch tutorial
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE
1. Structure of project
2. Step to do
– Create Spring Boot project
– Add needed dependencies
– Configure datasource for Batch Job Repository
– Create Step:Reader, Processor, Writer
– Configure Batch Job
– Create a Controller for launch Job
– Run & Check result
3. Demo Video
1. Create Spring Boot project
2. Add needed dependencies
– Add dependencies: Web MVC, Spring Batch, PostgreSQL
3. Config datasource for Batch Job repository
4. Create Step:Reader, Processor, Writer
public interface ItemReader: Strategy interface for providing the data.
Implementations are expected to be stateful and will be called multiple times for each batch, with each call to read() returning a different value and finally returning null when all input data is exhausted.
Implementations need not be thread-safe and clients of a ItemReader need to be aware that this is the case.
A richer interface (e.g. with a look ahead or peek) is not feasible because we need to support transactions in an asynchronous batch.
public interface ItemProcessor: Interface for item transformation. Given an item as input, this interface provides an extension point which allows for the application of business logic in an item oriented processing scenario. It should be noted that while it’s possible to return a different type than the one provided, it’s not strictly necessary. Furthermore, returning null indicates that the item should not be continued to be processed.
public interface ItemWriter: Basic interface for generic output operations. Class implementing this interface will be responsible for serializing objects as necessary. Generally, it is responsibility of implementing class to decide which technology to use for mapping and how it should be configured.
5. Enable Spring Batch
In main class, use @EnableBatchProcessing: Enable Spring Batch features and provide a base configuration for setting up batch jobs in an @Configuration class.
6. Configure Batch Job
Create a configuration file and enable Spring batch via annotations:
7. Create a Controller for launch Job
Create a simple controller JobLauncherController with 1 @RequestMapping(“/launchjob”)
org.springframework.batch.core.launch.JobLauncher: Simple interface for controlling jobs, including possible ad-hoc executions, based on different runtime identifiers. It is extremely important to note that this interface makes absolutely no guarantees about whether or not calls to it are executed synchronously or asynchronously. The javadocs for specific implementations should be checked to ensure callers fully understand how the job will be run.
org.springframework.batch.core.JobParameters: Value object representing runtime parameters to a batch job. Because the parameters have no individual meaning outside of the JobParameters they are contained within, it is a value object rather than an entity. It is also extremely important that a parameters object can be reliably compared to another for equality, in order to determine if one JobParameters object equals another. Furthermore, because these parameters will need to be persisted, it is vital that the types added are restricted. This class is immutable and therefore thread-safe.
8. Run & Check result
– Maven build:clean install
– Run project with mode: Spring Boot App
Make a request:http://localhost:8080/launchjob
IV. Source code