Search code examples
javaspringspring-batch

How to implement custom controller that will allow me to stop a step in Spring Batch


Currently the Spring Batch framework does not support an official method to pause a step.

This presents a challenge for me and I was hoping to get some insight into how I could go about configuring such logic?

I have an external service that decides if the current job should stop a specific step. There are a large number of reasons in my system that require such fine-grained control over individual steps of a running job.

My initial thoughts into how I could configure pausing of a step is to do one of two things:

  1. Would be to sleep the specific step thread until the control flow signals to my service to start running that thread again.
  2. Override the Step Interface and include a method that would allow me to pause the step via some class property(say a boolean value). Sadly for this case I am unsure of which classes I should consider overriding.

The current Flow class abstract spring batch provides is not good enough as this is static to the job where in my case I have another service deciding this hence Flow is not sufficient.

The ultimate goal being that the JobRepository spring batch is using has the proper status set as well as the actual step not running which I have found to be the tricky part of such a feature. Re-starting my steps entirely is another possibility but I would prefer to avoid this as it would result in wasted work unless I re-start the step following the last read and write count which I have also not fully understood.

Thank you.


Solution

  • I have an external service that decides if the current job should stop a specific step.

    You can use FlowBuilder#stopAndRestart(JobExecutionDecider decider) to stop the flow at a particular step and restart the job later. The custom decision can be implemented in the JobExecutionDecider.

    You can also stop the job at a particular step and start at another step using FlowBuilder#stopAndRestart(Step restart).