π Spring Batch – Beginner’s Guide with Real-Time Example
Ever wondered how large amounts of data are processed in batches, like a boss? π
Welcome to Spring Batch – your friend when you need reliable, fast, and scalable batch processing in Java!
1️⃣ What is Spring Batch? π€
- A framework to process large volumes of data efficiently.
- Handles batch jobs, transactions, retries, skip logic, and chunk-based processing.
- Perfect for ETL jobs, report generation, invoice processing – basically, anything your database hates if you run it all at once π.
2️⃣ Important Concepts & Flow π
Core Components:
- Job – The whole batch process (like a movie π¬).
- Step – A phase of a job (like a scene in that movie).
- ItemReader – Reads data from source (DB, CSV, API…think Sherlock reading clues π΅️♂️).
- ItemProcessor – Processes/validates data (Sherlock deduces π).
- ItemWriter – Writes data to destination (He reports findings ✉️).
Flow:
Controller/Scheduler ➡ JobLauncher ➡ Job ➡ Step ➡ Reader ➡ Processor ➡ Writer ➡ Job Complete π
3️⃣ Key Annotations & Configurations ⚙️
| Annotation | What it does | Funny Analogy |
|---|---|---|
| @EnableBatchProcessing | Boots up Spring Batch | Like turning on the engine π |
| @Configuration | Defines beans and jobs | Architect drawing blueprints π️ |
| @Bean | Declares a component | “Hey Spring, manage this for me!” π·️ |
| @JobScope / @StepScope | Scope for job or step beans | Beans with VIP access π« |
| @Component | Marks a regular Spring component | Ordinary citizen π§πΌ |
Dependencies:
<!-- Maven -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
4️⃣ Real-Time Example πΌ
Scenario: Daily employee salary update from CSV to DB.
- Controller or Scheduler triggers Job
- JobLauncher launches Job
- Step reads CSV (ItemReader)
- Step processes salary (ItemProcessor)
- Step writes to DB (ItemWriter)
- Job finishes successfully or fails with retry logic
5️⃣ Flow Diagram Representation π
+-------------+ +------------+ +------------+ +-----------+
| Controller /| | JobLauncher| | Job | | Step |
| Scheduler | ---> | ---------->| ---> | ---------->| ---> | ---------->|
+-------------+ +------------+ +------------+ +-----------+
| |
v v
+---------+ +---------+
| Reader | | Writer |
+---------+ +---------+
|
v
+-----------+
| Processor |
+-----------+
|
v
Job Completed π
6️⃣ Example Program – CSV to Database Batch Job π»
Scenario: Daily employee salary update from CSV to DB.
// 1️⃣ Entity
@Entity
public class Employee {
@Id
private Long id;
private String name;
private Double salary;
// getters & setters
}
// 2️⃣ Batch Configuration
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource dataSource;
// Reader - read CSV
@Bean
@StepScope
public FlatFileItemReader reader(@Value("#{jobParameters['filePath']}") String path) {
return new FlatFileItemReaderBuilder()
.name("employeeReader")
.resource(new FileSystemResource(path))
.delimited()
.names("id", "name", "salary")
.targetType(Employee.class)
.build();
}
// Processor - validate salary
@Bean
public ItemProcessor processor() {
return emp -> {
if(emp.getSalary() < 0) throw new IllegalArgumentException("Salary cannot be negative!");
return emp;
};
}
// Writer - save to DB
@Bean
public JdbcBatchItemWriter writer() {
return new JdbcBatchItemWriterBuilder()
.dataSource(dataSource)
.sql("INSERT INTO employee (id, name, salary) VALUES (:id, :name, :salary)")
.beanMapped()
.build();
}
// Step
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.chunk(5)
.reader(reader(null))
.processor(processor())
.writer(writer())
.build();
}
// Job
@Bean
public Job importEmployeeJob(JobCompletionNotificationListener listener) {
return jobBuilderFactory.get("importEmployeeJob")
.listener(listener)
.flow(step1())
.end()
.build();
}
}
// 3️⃣ Job Launcher from Controller
@RestController
public class JobLauncherController {
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job importEmployeeJob;
@GetMapping("/launchjob")
public String launchJob() throws Exception {
JobParameters params = new JobParametersBuilder()
.addString("filePath", "/path/to/employee.csv")
.addLong("time", System.currentTimeMillis())
.toJobParameters();
jobLauncher.run(importEmployeeJob, params);
return "Batch Job has been invoked";
}
}
✅ What happens:
- Visit
http://localhost:8080/launchjoband your batch job triggers. - CSV data is read, validated, and written to DB in chunks.
- Spring Batch manages transactions, retries, and job metadata automatically.
7️⃣ Extra Fun Facts & Tips π€
- Spring Batch automatically manages transaction, chunk, retry, and skip logic.
- You can restart failed jobs without re-processing completed data.
- Jobs can be triggered via Scheduler, Cron, or API endpoint.
8️⃣ Wrapping Up π
Spring Batch might look complex initially, but it’s like a conveyor belt for your data – you just need to set up the machinery and let it run! π
π‘ Tip: Always use chunk-based processing for large files, otherwise your JVM might cry π.
Comments
Post a Comment