Spring Batch Job

  • Last updated Apr 25, 2024

A Job is an entity that consists of one or more steps. Each steps has one ItemReader, one ItemProcessor, and one ItemWriter. A Job also contain JobRepository where metadata about the currently running process is stored. A Job is launched using JobLauncher.


Job Configuration

A Job is configured either using an XML file or Java-based configuration. This configuration is called Job Configuration. A Job also allows for global configurations for properties such as restartability for all steps.

Spring Batch provides SimpleJob class which is a default simple implementation of the Job interface with some standard functions on top of Job. Java based configuration Job can be created using builders as shown below.

Bean
public Job myJob(JobCompletionNotificationListener listener, Step step1) throws Exception {

    return this.jobBuilderFactory.get("myJob")
    .incrementer(new RunIdIncrementer())
    .listener(listener)
    .flow(step1)
    .end()
    .build();

}

The job configuration has:

  • The name of the Job.
  • Definition of steps.
  • Job restartable or not.


Job Hierarchy

Given below is a diagram of Job Hierarchy:




JobInstance

JobInstance is a single occurence of a logical job run. If a Job run of a particular day fails and is run again the next day, the Job run is still of that particular first day and not of the next day when it was run again. A single JobInstance may run multiple times but at a given time only one JobInstance corresponding to a particular job and identifying job parameters can run.

The use of a new JobInstance means "start from the beginning", and the use of an already existing instance means availability of the state from previous JobInstance and starting from where you left off.


JobParameters

A JobParameters object contains a set of parameters used for identifying a particular job from other jobs. However, a job can also be started without JobParameters that identifies a Job. The parameters may also be used as reference data while running the Job. A Job can be one but may have different JobParameters which gives control to a developer to decide how to define a JobInstance.


JobExecution

JobExecution is a single attempt of running a job. A JobInstance is considered to be complete only when the job execution completes successfully. If a JobExecution ended up with failure then the JobInstance is not considered to be success. If a JobInstance failed the first time and run again with the same parameters as the first run, a new JobExecution is created but the JobInstance is still only one.

JobExecution contains information about a job run and several other properties which must be persisted. Some of the properties are listed below:

Property
Definition
startTime
It indicates the job started time in java.util.Date form. If the Job is not started the field value is empty.
endTime
It indicates the job completed time in java.util.Date form. The Job may be completed with or without success. If the Job is not completed yet, the field value is empty.
exitStatus
It indicates the result of the JobExecution with an exit code. If the Job is not completed yet, the field value is empty.
createTime
It is the time in java.util.Date. It indicates when the job was created. A Job always have a createTime even if the job is not started yet. It is required by the framework for managing Job Level ExecutionContext.
lastUpdated
It is the time in java.util.Date. It indicates when the job was updated last time. This field is empty if the Job is not started yet.
executionContext
It is a container for user'data that needs to be persisted between executions of the Job.
failureExceptions
Defines the list of exceptions that was encountered during the execution of a Job.