Updated March 30, 2023
Introduction to Spring Batch Processing
Spring batch processing is characterized by non-interactive, frequently long-running background execution, and bulk-oriented is utilized in almost every industry and for a wide range of tasks. Batch processing can be data or computationally heavy, run sequentially or in parallel, and be started using different invocation models, such as ad hoc, scheduled, or on-demand. Spring Batch is a lightweight, feature-rich framework that makes it easier to create reliable batch applications.
What is spring batch processing?
- Any developer should be knowledgeable and comfortable with the fundamental notions of batch processing.
- It introduces spring Batch’s fundamental concepts and phrases in batch processing.
- A Job, which is made up of numerous Steps, usually encapsulates a batch operation. ItemProcessor, ItemReader, and ItemWriter are usually present in each Step.
- A JobLauncher executes a job, while a JobRepository store’s metadata about jobs configured and executed.
- Each Job can have several JobInstances, each of which is defined by its own set of job parameters. For example, a JobExecution is the name for a single run of a Job Instance.
- Every Job is made up of one or more steps, each of which is an independent, specific portion of a batch Job.
- A single StepExecution, similar to a Job, represents a single attempt to perform a Step. StepExecution keeps track of current and exit statuses, start and finish times, and pointers to the corresponding Step and JobExecution instances.
Spring batch processing Application setup
Below examples shown to set up the project of spring batch processing are as follows.
- Create project template of spring batch processing by using spring initializer
- In the below step, we have provided project group name as com.example, artifact name as SpringBatchProcessing, project name as SpringBatchProcessing, 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, spring data JPA, and PostgreSQL driver dependency in the below project to implement the spring batch processing project.
Group – com.example
Artifact name – SpringBatchProcessing
Name – SpringBatchProcessing
Spring boot – 2.6.0
Project – Maven
Java – 8
Package name – com.example.SpringBatchProcessing
Project Description – Project for SpringBatchProcessing
Dependencies – spring web, PostgreSQL driver, spring batch, Spring data JPA.
- 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 admin dependency in the spring batch 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.
<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.
- Create data layer –
Code –
@Entity(name = "student")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners (AuditingEntityListener.class)
public class Stud
{@Id
@Column(name = "id")
@GeneratedValue (strategy = GenerationType.AUTO)
private int stud_id;
@Override
public String toString() {
return "stud_id: " + stud_id;
}
}
- Create repository layer –
Code –
public interface StudentRepo extends JpaRepository<Stud, Long> {
}
- Create processor –
Code –
public class StudProcessor implements ItemProcessor {
private static final Logger l = (Logger) LoggerFactory.getLogger(StudProcessor.class);
public String process(final Stud st) throws Exception {
final String firstName = st.getStud_ID ().toUpperCase();
String student = null;
l.info("Convert (" + st + ") into (" + student + ")");
return student;
}
@Override
return null;
}
}
- Create configuration layer –
Code –
@Configuration
@EnableBatchProcessing
public class Config {
@Autowired
public JobBuilderFactory jbf;
@Autowired
public StepBuilderFactory sbf;
@Bean
public FlatFileItemReader<Stud> reader() {
return new FlatFileItemReaderBuilder<Stud>()
.name("studReader")
.resource(new ClassPathResource("stud.csv"))
.delimited()
.names(new String[]{"Stud_id"})
.fieldSetMapper(new BeanWrapperFieldSetMapper<>() {{
setTargetType(Stud.class);
}})
.build();
}
@Bean
public RepositoryItemWriter<Stud> writer()
{
RepositoryItemWriter<Stud> iwriter = new RepositoryItemWriter<>();
CrudRepository<Stud, ?> studRepo = null;
iwriter.setRepository(studRepo);
iwriter.setMethodName("Save");
return iwriter;
}
- Create controller layer
Code –
@RestController
@RequestMapping(path = "/batch")// Root path
public class Controller
{
@Autowired
private JobLauncher jl;
@Autowired
private Job j;
@GetMapping(path = "/start")
public ResponseEntity<String> startBatch () throws org.springframework.batch.core.repository.JobRestartException
{
JobParameters p = new JobParametersBuilder()
.addLong("started", System.currentTimeMillis()).toJobParameters();
return new ResponseEntity<>("Process of batch is started", HttpStatus.OK);
}
}
- Configure application.properties file –
Code
server.port=8080
spring.batch.job.enabled=false
- Run the application –
Spring batch processing Framework
- Spring Batch is a lightweight, feature-rich batch framework that enables the creation of dependable batch applications that are critical to enterprise systems’ day-to-day operations.
- Batch operations that handle large amounts of data can take advantage of the framework in a highly scalable way.
- Spring Batch is based on the Spring Framework; we should be familiar with the capabilities and functions of spring.
- Spring batch has the benefit of having few project dependencies, making it easier to get up and running quickly.
Key Concepts and Terminology
- The ExecutionContext is saved by Spring Batch, which is useful if you need to restart a batch job.
- All of this persistence is made possible through the JobRepository mechanism in Spring Batch. In addition, it offers CRUD operations for JobLauncher, Job, and Step instantiations.
- A JobExecution is retrieved from the repository after a Job is launched, and StepExecution and JobExecution instances persist during the execution process.
- To develop a spring boot processing application, we need to add spring web, spring batch, spring data JPA and PostgreSQL driver dependency in the pom.xml file.
Conclusion
Any developer should be knowledgeable and comfortable with the fundamental notions of batch processing. Spring Batch is a lightweight, feature-rich batch framework. Spring batch is characterized by non-interactive, frequently long-running background execution, and bulk-oriented is utilized in almost every industry and for a wide range of tasks.
Recommended Articles
This is a guide to Spring Batch Processing. Here we discuss the Spring batch processing Framework along with the Key Concepts and Terminology. You may also have a look at the following articles to learn more –