Spring Batch - Job Scheduling and Job Execution
Last Updated :
04 Jan, 2025
Spring Batch is a framework for building robust and scalable batch-processing applications in Java. It provides features for job scheduling and job execution, making it well-suited for tasks such as data extraction, transformation, and loading (ETL), report generation, and more. In this explanation, I'll walk you through the process of scheduling and executing a simple Spring Batch job using source code examples.
Prerequisites
Before you start, make sure you have the Spring Batch and Spring Boot dependencies in your project. You can add them to your pom.xml (if you're using Maven) or build.gradle (if you're using Gradle).
<!-- Spring Boot Starter Batch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<!-- Spring Boot Starter Web (for scheduling via HTTP) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Step-by-Step Implementation
Step 1: Create a Batch Job
Let's create a simple batch job that reads data from a CSV file, processes it, and writes the results to another file. You'll need to create an item reader, processor, and writer;
Java
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public ItemReader<String> itemReader() {
// Implement your item reader logic (e.g., read from a CSV file).
// Return a reader for the input data.
}
@Bean
public ItemProcessor<String, String> itemProcessor() {
// Implement your item processor logic (e.g., data transformation).
// Return a processor for data processing.
}
@Bean
public ItemWriter<String> itemWriter() {
// Implement your item writer logic (e.g., write to a file or database).
// Return a writer for output data.
}
@Bean
public Step step() {
return stepBuilderFactory.get("step")
.<String, String>chunk(10)
.reader(itemReader())
.processor(itemProcessor())
.writer(itemWriter())
.build();
}
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(step())
.build();
}
}
/*
In this code, we define a batch job with a step that includes an item reader, processor, and writer.
You should replace the placeholder methods with your specific logic for reading, processing, and writing data.
*/
Step 2: Schedule the Batch Job
Now, let's schedule the batch job to run at specific intervals using Spring's @Scheduled annotation. We'll create a simple scheduler class.
Java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class BatchScheduler {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
@Scheduled(cron = "0 0 0 * * ?") // Schedule at midnight daily
public void performBatchJob() throws Exception {
JobParameters params = new JobParametersBuilder()
.addString("JobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters();
jobLauncher.run(job, params);
}
}
/*
In this code, we use the @Scheduled annotation to schedule the performBatchJob method to run daily at midnight.
Inside this method, we launch the Spring Batch job with appropriate parameters.
*/
Step 3: Configure Application Properties
Finally, you'll need to configure your application properties, such as the data source and file paths. You can do this in your application.properties or application.yml file.
# Data source configuration
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=myuser
spring.datasource.password=mypassword
# Job-related properties
spring.batch.job.enabled=true
#Make sure to replace the database URL, username, and password with your specific database configuration.
Step 4: Run the Spring Boot Application
Now, you can run your Spring Boot application. The batch job will be scheduled to run daily at midnight, and it will execute the defined batch processing logic.
Conclusion
This is a basic example of scheduling and executing a Spring Batch job. You can customize it to fit your specific batch processing requirements, such as handling failures, monitoring, and logging. Additionally, consider deploying your application to a production environment and configuring more advanced scheduling options as needed.
Similar Reads
Spring Boot - Scheduling
Spring Boot provides the ability to schedule tasks for execution at a given time period with the help of @Scheduled annotation. This article provides a step by step guideline on how we can schedule tasks to run in a spring boot application Implementation:It is depicted below stepwise as follows:Â St
4 min read
Spring Security - Role Based Authentication
Authentication is when anyone wants to access your Rest API they need some Authorization like a Username, Password, and token kind of. So Spring Boot Security has a Spring Boot 6.2.0 version. In the lower version Some Methods are deprecated in spring Security that's why a new thing comes into the pi
4 min read
Difference between Spring and Spring Boot
Spring Spring is an open-source lightweight framework that allows Java developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier
4 min read
Appointment Scheduling System in Spring Boot
The Appointment Scheduling System includes operations such as sending your appointment request to the admin. By providing the required details for each appointment request, a random string with the prefix "RQ" is created for tracing your appointment. In this article, we explain the Appointment Sched
15+ min read
Spring Data JDBC Extensions
In this article, We will explore the realm of Spring Data JDBC extensions. Discover how they enhance the functionalities of the Spring Data JDBC module. These extensions offer a range of tools that enable developers to handle database interactions, with skill-making tasks, like simplifying queries e
3 min read
Batch Processing - MongoDB to CSV Export using Spring Batch
Batch processing is a common requirement while dealing with large volumes of data. Batch processing plays an important role in handling large datasets efficiently using chunks or batches. Spring framework provides a flexible framework for building batch-processing applications in Java. Steps to Expo
5 min read
Batch Processing With Spring Cloud Data Flow
the Spring Cloud Data Flow is an open-source architectural component, that uses other well-known Java-based technologies to create streaming and batch data processing pipelines. The definition of batch processing is the uninterrupted, interaction-free processing of a finite amount of data. Component
3 min read
Monitoring and Logging in Spring Boot
Spring Boot is one of the most popular application development frameworks among developers. Spring boot can be used to develop standalone Java enterprise-level applications. Spring framework also provides features for ease of development and better security and performance in spring applications. Th
6 min read
Configuring Multiple Spring Batch Jobs in a Spring Boot Application
Spring Batch serves as a robust framework within the Spring ecosystem, specifically tailored for managing batch processing tasks efficiently. It's designed to tackle big data jobs efficiently and comes with handy tools to make batch application development a breeze. In the context of a Spring Boot a
10 min read
Spring Security Interview Questions and Answers
Spring Security is a highly flexible and customizable security framework designed for Java applications, particularly those developed using the Spring Framework. It offers extensive security services for enterprise-level Java EE-based software applications. At its Core, Spring Security is concerned
15+ min read