Open In App

Spring Batch – Job Scheduling and Job Execution

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads