0

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?

One Developer
  • 99
  • 5
  • 43
  • 103
  • 1
    Make the job restartable and kick off the job again, it will start from where it left of. – M. Deinum Mar 17 '22 at 09:24
  • do you mean rerun the job? Our business needs us to retry the job instead. Job is very well restartable still. – One Developer Mar 17 '22 at 10:27
  • 1
    I indeed mean to retun the job, it will start where it left of. Chunks don't fail (generally) only parts of the chunk and when they fail there generally is no sense in retrying them. – M. Deinum Mar 17 '22 at 12:35
  • Is there a way to rerun the chunk? Especially when it fails due to SQL server connection failure - I have implemented retry-limit without any luck – One Developer Mar 17 '22 at 14:10
  • 1
    Why. Kick off the job again and it will re-run and start where it left. The chunk will retry all individual elements when something fails by default already. – M. Deinum Mar 17 '22 at 14:12
  • Jobs are configured in the enterprise scheduling system and requires additional approval to implement the re-run in the scheduling system. Restart the job/chunk is under our control. I have posted the original question here - https://stackoverflow.com/questions/71089219/sql-server-returned-an-incomplete-response-the-connection-has-been-closed-nes – One Developer Mar 17 '22 at 14:39
  • 1
    Where did I say you need to use that system, I stated you should restart the job. You can create something on the client that restarts failed jobs. I also saw in your other question you are using DBCP1 I strongly suggest to drop that and use the default HikariCP which is much better (and still actively maintained). – M. Deinum Mar 17 '22 at 14:50
  • Sure, we will give a try with Hikari. Is there a way to restart the job by itself in case of any failure? Without having to trigger again? – One Developer Mar 17 '22 at 15:03
  • 1
    You always have to trigger the job, but you can do that quite easily in java with the possibilities in Spring Batch. – M. Deinum Mar 17 '22 at 15:07

0 Answers0