Understanding Spring Batch Core Components
Spring Batch is a powerful framework designed for processing large volumes of data in batch jobs. To build a solid foundation with Spring Batch, it's essential to understand its core components and how they interact. These include:
- JobInstance
- JobExecution
- StepExecution
- JobRepository
- JobLauncher
If you're new to Spring Batch, start with our complete guide on importing CSV to database using Spring Batch.
1. JobInstance
A JobInstance represents a logical run of a job with a specific set of job parameters. It is created when a job is started with unique parameters. If you run the same job with the same parameters again, the existing instance is reused.
JobParameters parameters = new JobParametersBuilder()
.addString("run.id", "1")
.toJobParameters();
jobLauncher.run(myJob, parameters);
2. JobExecution
A JobExecution is a single attempt to run a JobInstance. If a job fails and is restarted, a new JobExecution is created but associated with the same JobInstance.
It tracks status, start time, end time, and exit status. Learn how to hook into these events using a JobExecutionListener in Spring Batch.
3. StepExecution
Each job contains steps. A StepExecution represents a single attempt to run a step as part of a JobExecution. Each time a step is run, a new StepExecution is created.
StepExecution tracks details like read count, write count, commit count, etc.
4. JobRepository
The JobRepository is a critical component responsible for persisting the meta-data of jobs (JobInstance, JobExecution, StepExecution). It interacts with the database to store and retrieve this data.
It's configured in the batch configuration and is required for job tracking and restart capabilities.
@Configuration
@EnableBatchProcessing
public class BatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public JobRepository jobRepository() throws Exception {
return new JobRepositoryFactoryBean().getObject();
}
}
5. JobLauncher
The JobLauncher is the interface used to launch a job. You provide it with a Job and JobParameters, and it creates a JobInstance and starts execution.
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
public void runJob() throws Exception {
JobParameters params = new JobParametersBuilder()
.addLong("time", System.currentTimeMillis())
.toJobParameters();
jobLauncher.run(job, params);
}
How They Interact
Here's how these components work together:
- You call
JobLauncher.run()
with a job and parameters. - Spring Batch checks the
JobRepository
for an existingJobInstance
. - If none exists, a new
JobInstance
is created. - A new
JobExecution
is created for this run. - For each step in the job, a
StepExecution
is created and tracked. - All this metadata is stored and updated in the
JobRepository
.
Want to execute simple custom logic like file cleanup or metadata insertion? Try using a Tasklet-based Step in Spring Batch.
Conclusion
Understanding these components is key to mastering Spring Batch. With this knowledge, you can build jobs that are restartable, traceable, and resilient in real-world batch processing scenarios.
🔗 Related Posts
📥 Import CSV to DB
Create a job that reads customer data from a CSV and writes it into a database table.
📤 Export DB to CSV
Learn how to use JdbcCursorItemReader and FlatFileItemWriter to export data.
🔄 ItemProcessor in Action
Transform data between read and write phases with a custom ItemProcessor.
🚫 Error Handling & Skip Policy
Handle faulty records and keep the job running with Spring Batch’s skip logic.