Use a JobListener for logic that needs to run once at the beginning or end of an entire batch job, such as setting up or tearing down resources used by multiple steps. Use a StepListener for logic that needs to run before and after a specific step completes, like writing a footer to a file after a data loading step or handling errors that occur within that particular step.

Use a JobListener for logic that needs to run once at the beginning or end of an entire batch job, such as setting up or tearing down resources used by multiple steps. Use a StepListener for logic that needs to run before and after a specific step completes, like writing a footer to a file after a data loading step or handling errors that occur within that particular step.
Use a JobListener for:
Pre-job initialization:
Tasks like initializing connection pools, starting a timer for the entire job, or setting up a shared context that all steps will use.
Post-job cleanup:
Tasks like cleaning up temporary files, sending summary reports, or releasing shared resources after the job has finished.
Overall job status logging:
Capturing the overall start and end times of the entire batch process.
Use a StepListener (specifically StepExecutionListener) for:
Step-specific setup:
Any setup required only before a particular step starts, like preparing a file for writing only within that step.
Step-specific teardown:
Cleanup tasks that are unique to a step, such as writing a footer to a flat file after a step has processed its items.
Step-level error handling:
Catching or logging errors that occur specifically within a single step.
Interacting with the StepContext:
Modifying or reading data from the context that is specific to a single step’s execution.

SimpleJobListener.java

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.stereotype.Component;

@Component
public class SimpleJobListener implements JobExecutionListener {

    @Override
    public void beforeJob(JobExecution jobExecution){
        System.out.println("Before Job Starts - "+ jobExecution.getJobInstance().getJobName());
        System.out.println("Job Params - "+ jobExecution.getJobParameters());
        System.out.println("Execution Contenxt- "+ jobExecution.getExecutionContext());
        jobExecution.getExecutionContext().put("Name", "Mugil");
    }

    @Override
    public void afterJob(JobExecution jobExecution){
        System.out.println("After Job Ends - "+ jobExecution.getJobInstance().getJobName());
        System.out.println("Job Params - "+ jobExecution.getJobParameters());
        System.out.println("Execution Contenxt- "+ jobExecution.getExecutionContext());
    }
}

SimpleStepListener.java

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.stereotype.Component;

@Component
public class SimpleStepListener implements StepExecutionListener {
    @Override
    public void beforeStep(StepExecution stepExecution){
        System.out.println("Before Step Run " + stepExecution.getStepName());
        System.out.println("Job Context " + stepExecution.getJobExecution().getExecutionContext());
        System.out.println("Step Context " + stepExecution.getExecutionContext());
        stepExecution.getExecutionContext().put("Name", "Vannan");
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution){
        System.out.println("After Step Run " + stepExecution.getStepName());
        System.out.println("Job Context " + stepExecution.getJobExecution().getExecutionContext());
        System.out.println("Step Context " + stepExecution.getExecutionContext());
        return ExitStatus.COMPLETED;
    }
}

Comments are closed.