Spring Batch Step

A Step is an independent object in the logical order of a batch job. Every job is made up of one or more steps. A step contains information to manage actual job processing. A step can be simple that requires little or no code, such as loading of data from a file and writing the data into a database. A step can also be complex that requires implementation of complex business principles as part of the processing.

Every step has an individual StepExecution and each StepExecution corresponds to a unique JobExecution.


StepExecution

StepExecution is a single attempt of executing a step. Everytime a step is run, a new StepExecution is created. If a step fails, no StepExecution is created for its corresponding step. StepExecution is created only when a step successfully starts and runs. There is a StepExecution class in Spring Batch that represents step executions. Each StepExecution contains data, such as the step start and end times, data commits, data rollback counts, and its corresponding step and JobExecution. There is also ExecutionContext for every StepExecution. The ExecutionContext contains state information that may be required to restart the execution. It also contains any other data that must be available across batch runs.

The properties of StepExecution are listed in the following table:

Property
Definition
startTime
It indicates the execution start time in java.util.Date form.
endTime
It indicates the execution end time in java.util.Date form.
Status
It indicates the status of the execution. Example BatchStatus.STARTED, BatchStatus.FAILED, BatchStatus.COMPLETED.
exitStatus
It indicates the execution exit status. It contains exit status code.
readCount
It indicates the number of data item read successfully.
readSkipCount
It indicates the number of data item skipped due to read failure.
writeCount
It indicates the number of data item written successfully.
writeSkipCount
It indicates the number of data items skipped due to write failure.
processSkipCount
It indicates the number of data skipped due to process failure.
filterCount
It indicates the number of data items filtered by ItemProcessor.
commitCount
It indicates the number of data items committed successfully.
rollbackCount
It indicates the number of data rolled back successfully.

ExecutionContext

ExecutionContext is a list of key-value pairs that is used to persist data with availability in a StepExecution object or a JobExecution object. It is similar to JobDataMap of Quartz.

When processing a batch job, the framework persists ExecutionContext at several commit points, allowing ItemReader to store its running state data. This helps to track down to start processing of data from the point where it ended during the last run in case of run failure due to problems like power outage.

To put the current id number of read data into the context is shown below:

executionContext.putLong(getKey( ID_NUMBER ), reader.getPosition());

To get the previously persisted id number from the context:

if(executionContext.containsKey(getKey( ID_NUMBER ))) {
    String idNumber = executionContext.getString(getKey( ID_NUMBER ));
}

It is also important to remember that there is at least one ExecutionContext for every JobExecution and every StepExecution. Example given below:

ExecutionContext stepExecutionContext = stepExecution.getExecutionContext();

ExecutionContext jobExecutionContext = jobExecution.getExecutionContext();