'Spring batch : Retry in case of a failure

I have a Spring batch application and I want to retry the step/chunk in case of a failure

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;

import java.util.List;

public class StepResultListener implements StepExecutionListener {

    @Override
    public void beforeStep(StepExecution stepExecution) {
        System.out.println("Called beforeStep().");
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        System.out.println("Called afterStep().");
        List<Throwable> exceptions = stepExecution.getFailureExceptions();
        if(exceptions.isEmpty()) {
            return ExitStatus.COMPLETED;
        } else {
            System.out.println("This step has encountered exceptions");
            exceptions.forEach(th -> System.out.println("Exception has occurred in job"));
            return ExitStatus.FAILED;
        }
    }
}

How do I implement?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source