Updated March 28, 2023
Introduction to Spring Batch
- It is a small framework that was used to create batch applications that are utilized in enterprise applications.
- It is a lightweight, all-in-one framework that enables the creation of reliable batch applications which was critical to the day-to-day operations of systems.
- It has reusable functions for job processing statistics, logging, skip, transaction management, resource management, and task restart which are all important in processing huge entries.
What is Spring Batch?
- It includes more advanced technological capabilities and services, such as partitioning and optimization techniques, which allow for exceptionally high-performance and high-volume batch processes.
- Complex and simple volume batch jobs are taking advantage of the framework to handle big amounts of data.
- The classic architecture uses a job repository for scheduling new jobs and communicating with existing jobs.
- This task implementation contains multiple steps, each of which processes, reads, and writes data.
- The framework is taking care of the majority of the heavy lifting which was handled by SQLite.
- It is a feature-rich framework that makes it easier to create reliable batch applications.
- Through its partitioning and optimization approaches, it is also delivered more advanced technological services and features which was supporting high-performance and high-volume batch operations.
- In this processing is nothing but the processing mode in which a sequence of automated jobs is executed without any manual interaction of the user.
- This process will work with huge amounts of data and it will take more time to complete the process.
- It is extending the Spring Framework’s POJO-based development paradigm, which is familiar to all developers.
Spring Creating a Batch Service
The below example shows creating service is as follows.
- Create project template for service by using the spring initializer
In the below step we have provided project group name as com.example, artifact name as springbatch, project name as springbatch, and selected java version as 8.
In the below project, we have selected spring web, spring batch, and PostgreSQL driver dependency to implement the project.
Group – com.example
Artifact name – springbatch
Name – springbatch
Spring boot – 2.6.0
Project – Maven
Java – 8
Dependencies – spring web, PostgreSQL driver, spring batch.
Package name – com.example.springbatch
Project Description – Project for springbatch
- After generating project extract files and open this project by using spring tool suite –
After generating the project by using the spring initializer in this step we are extracting the jar file and opened the project by 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 a dependency to the project.
Code –
<dependency> -- Start of dependency tag.
<groupId>org.springframework.batch</groupId> -- Start and end of groupId tag.
<artifactId>spring-batch-test</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
- Create CSV file for processing –
In this step, we are creating a CSV file for batch processing.
Code –
1,ABC,Pune
2, PQR, Mumbai1,ABC,Pune
3, XYZ, Pune
- Configure spring batch –
In this step, we have to configure are as follows.
Code –
<batch:job id="springbatch">
<batch:step id="1">
<batch:tasklet>
<batch:chunk reader="reader" writer="writer"
processor="processor" commit-interval="1">
</batch:chunk>
</batch:tasklet>
</batch:step>
</batch:job>
Spring Batch Processing
- To process, we are using the project name as spring batch. In this project we have already configured XML, now we are processing the spring batch as follows.
- In the below example, we have created a class name as springbatchprocess to process.
Code –
public class springbatchprocess implements ItemProcessor {
public Process process(Process data) throws Exception {
System.out.println ("Spring batch processing" + data);
String fname = data.getFirstName ();
String city = data.getCity ();
data.setFirstName (fname.toUpperCase ());
data.setCity (lname.toUpperCase ());
return data;
}
Spring Batch Framework
- This framework will contain the below metadata table. All those tables closely match the java domain object.
- Batch_job_seq
- Batch_step_execution_seq
- Batch_job_execution_seq
- Batch_step_execution_context
- Batch_job_execution_context
- Batch_step_execution
- Batch_job_execution_params
- Batch_job_instance
- Batch_bob_execution
- The batch_job_instance table is used to hold all the info related to an instance of the job. The batch_job_execution_params table is used to hold all the info related to the parameters object.
- The batch_job_execution table is used to hold all the info related to the object of job execution.
- The batch_step_execution table is used to hold all the info related to the object of step execution.
- The batch_job_execution_context table is used to hold all the data related to the context of execution.
- The batch_step_execution_context table is used to hold all the data related to the step context of execution.
- To develop the framework we are using the project name as spring batch. In this project, we have already configured xml, now we are creating the framework as follows.
Code –
public class SpringBatchFramework {
public static void main(String[] args) {
String[] springConfig = { "spring/jobs/batch-demo.xml" };
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
JobLauncher jl = (JobLauncher) context.getBean("spring batch");
Job spring_job = (Job) context.getBean("demo");
try {
JobExecution exec = (JobExecution) jl.run (spring_job, jp);
System.out.println ("Status : " + ((org.springframework.batch.core.JobExecution) exec).getStatus ());
} catch (Exception e) {
e.printStackTrace ();
}
System.out.println ("Completed");
context.close();
}
}
Spring Batch Module
To develop the module we are using the project name as spring batch.
- Create module
Code –
@XmlRootElement (ModuleName = "SpringBatchModuel")
public class springbatchmodule {
private int stud_id;
private String fName;
@XmlAttribute (fName = "id")
public int getId() {
return stud_id;
}
@XmlElement (fName = "fname")
public String getFName () {
return fName;
}
public void setfName (String fName)
{
this.fName = fName;
}
@Override
public String toString() {
return "Report [stud_id=" + stud_id + ", first_name=" + fName + "]";
}
}
- Run the application
Conclusion
It is a feature-rich framework that makes it easier to create reliable batch applications. Spring batch task implementation contains multiple steps, each of which processes, reads, and writes data. It is a small framework that was used to create batch applications that are utilized in enterprise applications.
Recommended Articles
This is a guide to Spring Batch. Here we discuss What is spring batch is and examples for Creating Spring Batch Service along with its processing and modules. You may also have a look at the following articles to learn more –