Updated March 30, 2023
Introduction to Spring Batch Job
Spring batch job is nothing but to create batch processing routines in the JVM in a standardized manner. Each step can have a Read-Process-Write job or a single action, which is referred to as a tasklet. Read-Process-Write is the process of reading data from a source such as a database or a CSV file, processing it, and writing it to another source such as a database, CSV, or XML file.
What is a spring batch job?
- Tasklet refers to completing a single task or action, such as clearing connections or freeing resources after processing.
- A batch job has a certain number of steps. Batch job includes reusable functions for logging, job processing statistics, transaction management, task restart, skip, and resource management, which are all important in processing huge volumes of records.
- Simple as well as complex, high-volume batch processes can use the framework to process large amounts of data in a highly scalable manner.
- Spring batch jobs will adhere to the classic batch design, in which a job repository is responsible for scheduling and dealing with jobs.
- A task can have multiple steps, each of which usually follows the same pattern of reading input, processing it, and writing it.
How to use the batch job?
- Spring batch framework will take care of the majority of the heavy lifting for us. Particularly when it comes to the low-level persistent task of dealing with the jobs, which will be handled by sqlite.
- Reading and writing to files, reading from or writing to databases, altering data, creating reports, importing and exporting data, and other operations are included in batch job processing.
- Frequently, these processes must be linked together, or we design more sophisticated workflows in which we define which job steps can be done in parallel or in order, and so on.
- To develop a batch job, we are using the following tools are as follows.
- Maven
- Java
- Spring batch
- Spring core
- Spring tool suit
- PostgreSQL java driver
- We’re reading a CSV file with FlatFileItemReader, processing the data with CustomItemProcessor, and writing to an XML file using StaxEventItemWriter.
- The job that we wish to create is defined by this tag. The job’s ID is specified via the Id attribute. Multiple jobs can be defined in a single xml file.
- This tag is used to specify the various steps of a batch task in spring.
- Spring Batch Framework provides two main types of processing styles tasklet step oriented and chunk oriented reader. A spring bean is used to read data.
Create spring batch job
To create a spring batch job, we are using the project template of SpringBatchJob are as follows.
- Create spring batch job configuration, class –
Code –
@Configuration
public class SpringBatchJob {
@Autowired
public JobBuilderFactory jbf;
@Autowired
public StepBuilderFactory sbf;
@Bean
public Job processJob() {
return jbf.get ("processJob")
.incrementer (new RunIdIncrementer()).listener(listener())
.flow(orderStep1()).end().build();
}
@Bean
public Step orderStep1() {
return null;
return sbf.get("order").<String, String> chunk(1)
.reader(new Reader()).processor(new Processor())
.writer(new Writer()).build();
}
@Bean
public JobExecutionListener listener() {
return null;
return new JobCompletionListener();
}
}
- Define ItemProcessor interface class –
Code –
Public class SpringBatch implements ItemProcessor {
public /* implement ItemProcessor interface */ String process(String data) /* string type of data */ throws Exception /* throws exception code */ {
return data.toUpperCase();
}
}
- Create a class to implement itemWriter interface –
Code –
public class writer implements ItemWriter {
public void write(List messages) throws Exception {
for (String message : messages) {
System.out.println("data writing" + message);
}
}
- Define listener –
Code –
public class Listener extends JobExecutionListenerSupport {
public void afterJob(JobExecution jobExecution) {
if (jobExecution.getBatchStatus() == BatchStatus.COMPLETED) {
System.out.println("Spring batch job completed");
}
}
}
- Configure application.properties file –
Code –
spring.datasource.url=jdbc:postgresql://localhost:5432/springbatchjob
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.batch.job.enabled=false
- Run the application –
Spring Batch Job Scheduler
- Spring batch is accomplished by passing the spring task scheduler to cron expression. We can enable the scheduling by using the annotation name as @EnableScheduling.
- Spring batch scheduler is the framework available in the spring batch, which is used to run scheduled jobs at a specific time.
- One of the open-source batch processing frameworks is Spring Batch. We may need to run batch jobs frequently or on a set schedule while working in any application.
Project spring batch job
The below example shows steps to setup a project of the batch job is as follows.
- Create a project template of the batch job by using a spring initializer
- In the below step, we have provided project group name as com.example, artifact name as SpringBatchJob, project name as SpringBatchJob, and selected java version as 8. Also, we have defined the spring boot version as 2.6.0, defined the project as maven.
- We have selected spring web, spring batch, and PostgreSQL driver dependency in the below project to implement the batch job project.
Group – com.example Artifact name – SpringBatchJob
Name – SpringBatchJob Spring boot – 2.6.0
Project – Maven Java – 8
Package name – com.example.SpringBatchJob
Project Description – Project for SpringBatchJob
Dependencies – spring web, PostgreSQL driver, spring batch.
- After generating project extract files and open this project by using spring tool suite –
- After generating the project using the spring initializer in this step, we extract the jar file and open the project using the spring tool suite.
- After opening the project using the spring tool suite, check the project and its files –
- In this step, we are checking all the project template files. We also need to check maven dependencies and system libraries.
- Add dependency packages –
- In this step, we are adding the spring batch dependency in the spring batch scheduler project.
Code –
<dependency> -- Start of dependency tag.
<groupId>org.springframework.boot</groupId> -- Start and end of groupId tag.
<artifactId>spring-boot-starter</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
<dependency> -- Start of dependency tag.
<groupId>org.postgresql</groupId> -- Start and end of groupId tag.
<artifactId>postgresql</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
Conclusion
Spring batch job includes more advanced technical services and capabilities that will enable extremely high-volume and high-performance batch jobs through optimization and partitioning approaches. In addition, a batch job is used to create batch processing routines in the JVM in a standardized manner.
Recommended Articles
This is a guide to Spring Batch Job. Here we discuss How to use the spring batch job along with the project template of Batch Job. You may also have a look at the following articles to learn more –