Spring Batch – Get StepExecution details of the previous step in the tasklet that runs in the next step.


Usually when I need to print the Step Summary information of the Step Execution Information of my Chunk Based step, I create a tasklet step immediately after the chunk based step to print the summary of the main step like number of records processed, records skipped, records committed etc.

I use the below approach to get the details of the step information of the previous step executed.

@Component
public class SummaryTaskletClass implements Tasklet {
    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        JobExecution jobExecution = chunkContext
                                    .getStepContext()
                                    .getStepExecution()
                                    .getJobExecution();// Get the job execution from the chunkContext
        StepExecution stepExecution=null;
// jobExecution.getStepExecutions() will give all the step execution under a job
// Loop through them and get the filter them based on the Step Name you want to select
        for (StepExecution stepDetail:jobExecution.getStepExecutions()) {
            if(stepDetail.getStepName().equals("PreviousStepName")){
                stepExecution = stepDetail;
                break;
            }
        };
        System.out.println("********************************************************************************");
        System.out.println("**************          Job Run Statistics Summary                  ************");
        System.out.println("********************************************************************************");
        System.out.println("******* Load Start Time   : "+stepExecution.getStartTime());
        System.out.println("******* Load End Time     : "+stepExecution.getEndTime());
        System.out.println("******* Commit Count      : "+stepExecution.getCommitCount());
        System.out.println("******* Read Count        : "+stepExecution.getReadCount());
        System.out.println("******* Filter Count      : "+stepExecution.getFilterCount());
        System.out.println("******* Write Count       : "+stepExecution.getWriteCount());
        System.out.println("******* Read Skip Count   : "+stepExecution.getReadSkipCount());
        System.out.println("******* Write Skip Count  : "+stepExecution.getWriteSkipCount());
        System.out.println("******* Process Skip Count: "+stepExecution.getProcessSkipCount());
        System.out.println("******* Rollback Count    : "+stepExecution.getRollbackCount());
        System.out.println("******* Exit Code         : "+stepExecution.getExitStatus().getExitCode());
        System.out.println("******* Exit Message      : "+stepExecution.getExitStatus().getExitDescription());
        System.out.println("********************************************************************************");
        System.out.println("********************************************************************************");
        return RepeatStatus.FINISHED;
    }
}