Updated April 5, 2023
Introduction to Spring Boot Batch
Spring boot batch processing is used to execute the jobs; spring batch is a framework which is responsible for executing the jobs or processing the jobs. This jobs can consist of single or multiple steps and does most of the heavy loaded task by themselves. To use this, we can use the spring boot annotation over the class; also, we are required to add the dependency in order to use this while development. Spring batch provides us inbuilt functions which can be used to process a large number of record in the enterprise application; also, it provides us various features such as declarative IO, transaction management and many more.
Syntax:
As we know, the spring batch is used to process the jobs; for this, we have to make some configuration with some code that needs to be in place.
@Configuration
@EnableBatchProcessing
public class DemoBatchConfiguration {
// logic goes here ..//
}
As you can see in the above syntax, we have used the @EnableBatchProcessing to enable the spring batch into our application; we have used this with @Configuration to configure it. Here we will discuss the internal detail for @EnableBatchProcessing annotation, also what other configurations are needed to make the job processing work in spring boot using spring batch.
How Batch Work in Spring Boot?
As we already know, the spring batch is a lightweight framework that is responsible to and enables us to develop applications that are very important to perform the daily operations of any enterprise application. Using spring batch, we can use it is in built functions which is helpful to process the large data and many more others.
Below are some features of the spring batch, which are as follows:
- Transaction management
- Web-based administration interface
- Declarative I/O
- Start/Stop/Restart
- Chunk based processing
- Retry/Skip
Given below are the required steps to configure the spring batch into the application:
1. First, we have to download the spring boot project zip from the spring initializer by mentioning the required files there and import the project into the editor we want.
2. Second, we have to add the dependency into the pom or gradle file, which ever build tool we are using; if we do not add this, then the error occurs while accessing the annotation from the spring package.
Below we have mentioned the required dependency for the same for reference:
Example:
Code:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
<version>2.4.0</version>
</dependency>
3. After adding the required dependency into the pom.xml, now we have to add the configuration regarding the batch processing in our application.
Below we have mentioned the class reference that we want while using it:
Example:
Code:
@Configuration
@EnableBatchProcessing
public class DemoBatchConfiguration {
// logic goes here ..//
}
In this above class, we are using @EnableBatchProcessing to enable this for our application; now, we will see what this @EnableBatchProcessing annotation does for us.
- @EnableBatchProcessing: First, it enables batch processing in our application; we are using with this @Configuration annotation. Also, by the use f this annotation we, it internally adds so many beans, which is responsible for providing us so many jobs which can be used to provide support for jobs and also the lots of custom code that may be required, it just has to focus on the business logic only. Also, this annotation adds the factories class, which is needed.
4. In this, we have a reader, writer and processor(). All these beans need to be add in the DemoBatchConfiguration class to make use of them.
Example:
Code:
@Bean
public FlatFileItemReader<Person> reader() {
// logic here .. //
}
@Bean
public PersonItemProcessor processor() {
// logic here .. //
}
@Bean
public JdbcBatchItemWriter<Person> writer(DataSource dataSource) {
// logic here .. //
}
This code needs to be added in the ‘DemoBatchConfiguration’ to configure the reader, writer and processor.
5. We are trying to store the data in the h3 data base which means data will be lost once the server is stopped. we are not keeping data into memory by using the h2 data base; if you want to see data and store them into a particular file then you can follow the below link for reference.
https://www.baeldung.com/spring-boot-h2-database
6. The main class for spring boot will be the same; there will not be many changes or configurations.
Example:
Code:
import com.test.Traders.entity.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
public class TradersApplication {
public static void main(String[] args)
{
SpringApplication.run(TradersApplication.class, args);
System.out.println("Application running in the dev mode !!");
}
}
Output:
Points to remember while using the spring batch for processing:
- Spring batch is a light weighted framework which is used to process the jobs.
- We can start using the spring batch by adding the required dependency into the pom.xml or gradle file.
- Practical use like, we can import the data from CSV to excel file, this can be a heavy process, but by the use of spring batch, we can use its internal functions to avoid the heavy code as well.
- We have used the @ EnableBatchProcessing annotation; this annotation will be used in the class where we want to configure all the things related to the spring batch like the reader, writer and processor etc.
Conclusion
With its use, we can easily make use of batch processing; also, we can process a large amount of data easily; we do not need to write the custom code; we can just focus on the business logic. Some of the features we have already discussed using may also increase the overall performance of enterprise applications.
Recommended Articles
This is a guide to Spring Boot Batch. Here we discuss the introduction and how batch work in spring boot? For better understanding. You may also have a look at the following articles to learn more –