
Question:
I want to be able to stop a job when a timing threshold is met. There are 2 approaches I was thinking about. First was to stop the job in the afterStep. However, I do not want it to have a Stopped status if it is at the completion of the last step. Therefore, I am going with stopping it in the beforeStep. I tried experimenting with
public void beforeStep(StepExecution stepExecution) {
stepExecution.setStatus(BatchStatus.STOPPED);
return;
}
and
public void beforeStep(StepExecution stepExecution) {
stepExecution.setExitStatus(ExitStatus.STOPPED);
return;
}
Neither of these worked. Any suggestions?
Answer1:The problem with the above code is that stops the currently running step, not the entire job. Perhaps if you add and adjust the following code in the job-current-step configuration will terminate the job when the ExitStatus of the step is STOPPED and when runned again the job will start from the next step.<br />(Taken from <a href="http://docs.spring.io/spring-batch/reference/html/configureStep.html#configuringForStop" rel="nofollow">here</a>)
<step id="step1" parent="s1">
<stop on="STOPPED" restart="step2"/>
</step>
<br /><a href="http://docs.spring.io/spring-batch/reference/html/configureStep.html#batchStatusVsExitStatus" rel="nofollow">This</a> might also help solve the confusion between ExitStatus/BatchStatus.
About the timing threshold... As I understand so far you plan to measure time of the <strong>current step only</strong> (beforestep - afterstep), no matter how long previous steps were running. To calculate and the previous steps try adding a timestamp in JobParameters and check when that threshold is passed.
Answer2:In my case a needed to stop job from afterStep. So I returned ExitStatus.Unknown and catch from job like that:
step().on("UNKNOWN").end()
So the job finish without error and I can restart the entire job when i want, whitout "restartable" option.
Hope this can help you.
Answer3:i suggest looking at <a href="http://docs.spring.io/spring-batch/reference/html/repeat.html#completionPolicies" rel="nofollow">CompletionPolicies</a> especially the <a href="http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/repeat/policy/TimeoutTerminationPolicy.html" rel="nofollow">TimeoutTerminationPolicy</a>
Answer4:You can set a timer (or something similar) and when receive time-limit notitication stop the job as described in <a href="http://docs.spring.io/spring-batch/reference/html-single/index.html#stoppingAJob" rel="nofollow">Stopping a job</a>.<br />
If you want the job is completed normally even if a time-alarm were broadcasted during last step, you can do check (using <a href="http://docs.spring.io/spring-batch/reference/html-single/index.html#JobOperator" rel="nofollow">JobOperator
</a>) to query job progress.