Stopping a Job in the beforeStep in Spring Batch

旧城冷巷雨未停 提交于 2019-12-11 07:38:02

问题


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?


回答1:


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.
(Taken from here)

<step id="step1" parent="s1">
    <stop on="STOPPED" restart="step2"/>
</step> 


This 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 current step only (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.




回答2:


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.




回答3:


i suggest looking at CompletionPolicies especially the TimeoutTerminationPolicy




回答4:


You can set a timer (or something similar) and when receive time-limit notitication stop the job as described in Stopping a job.
If you want the job is completed normally even if a time-alarm were broadcasted during last step, you can do check (using JobOperator) to query job progress.



来源:https://stackoverflow.com/questions/22122059/stopping-a-job-in-the-beforestep-in-spring-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!