Understanding Spring Batch Core Components (Complete Guide)

Spring Batch Core Components Explained (Beginner to Advanced)

Spring Batch is a highly scalable, high-performance batch processing framework built on Spring. To effectively design and debug Spring Batch jobs, you must understand its core components and how they interact behind the scenes.

These components build the entire foundation of Spring Batch:

  • JobInstance
  • JobExecution
  • StepExecution
  • JobRepository
  • JobLauncher

Once you understand these, concepts like job restartability, metadata storage, step tracking, and job monitoring become very intuitive.

If you're new to Spring Batch, start with this practical guide: Importing CSV to Database Using Spring Batch.


1. JobInstance

A JobInstance represents a logical run of a job with a specific set of JobParameters. Spring Batch treats job name + parameters as a unique identity.

👉 If you run a job with the exact same parameters again, Spring Batch will reuse the existing JobInstance. If parameters differ, a new JobInstance is created.


JobParameters parameters = new JobParametersBuilder()
    .addString("run.id", "1")
    .toJobParameters();

jobLauncher.run(myJob, parameters);

Key Points:

  • A JobInstance is created only once per unique parameter set.
  • All execution attempts (failures/restarts) belong to the same JobInstance.
  • Stored in the database for restartability.

2. JobExecution

A JobExecution represents a single attempt to run a JobInstance. Every time you run or restart a job, a new JobExecution is created.

JobExecution tracks important runtime metadata:

  • Start time & end time
  • Status (STARTED, COMPLETED, FAILED)
  • Exit status
  • Failure exceptions

If you want to log runtime details or generate reports, integrate a listener: JobExecutionListener in Spring Batch.


3. StepExecution

Each JobExecution consists of one or more steps. A StepExecution represents a single attempt to run a step.

Every time a step runs (or is retried), a new StepExecution is stored in the database.

StepExecution tracks metrics such as:

  • Read count
  • Write count
  • Commit count
  • Skip count
  • Rollback count
  • Processing time

These metrics are extremely useful for monitoring data quality and job performance.


4. JobRepository

The JobRepository is the core persistence layer of Spring Batch. It stores metadata for:

  • JobInstance
  • JobExecution
  • StepExecution
  • ExecutionContext

Without a JobRepository, Spring Batch cannot:

  • Restart failed jobs
  • Track job history
  • Maintain checkpointing
  • Store step metrics

Example configuration:


@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 responsible for triggering job execution. When you call jobLauncher.run(), it:

  1. Creates or finds the JobInstance
  2. Creates a new JobExecution
  3. Delegates execution to the job

Typical usage:


@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 These Components Work Together

Here’s the complete execution flow inside Spring Batch:

  1. You call jobLauncher.run(job, parameters).
  2. Spring Batch checks the JobRepository for an existing JobInstance.
  3. If none exists, a new JobInstance is created.
  4. A new JobExecution is created for this specific run.
  5. For each step inside the job, a StepExecution is created.
  6. During execution, Spring Batch updates all metadata in JobRepository.
  7. If the job fails, it can be restarted because the metadata is persisted.

Want to execute simple custom logic like file cleanup, logging, or API calls? Try using a: Tasklet-based Step in Spring Batch.


Conclusion

Understanding Spring Batch core components—JobInstance, JobExecution, StepExecution, JobRepository, and JobLauncher—is essential for building scalable and restartable batch jobs.

With this foundational knowledge, you can confidently design real-world batch systems that are reliable, traceable, fault-tolerant, and production-ready.