简体   繁体   English

在 Spring Batch 的 beforeStep 中停止作业

[英]Stopping a Job in the beforeStep in Spring Batch

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.首先是在 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.因此,我打算在 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?有什么建议?

In my case a needed to stop job from afterStep.在我的情况下,需要从 afterStep 停止工作。 So I returned ExitStatus.Unknown and catch from job like that:所以我返回了 ExitStatus.Unknown 并从这样的工作中捕获:

step().on("UNKNOWN").end() 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.希望这可以帮到你。

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.也许如果您在 job-current-step 配置中添加和调整以下代码将在步骤的 ExitStatus 为 STOPPED 时终止作业,并且再次运行时,作业将从下一步开始。
(Taken from here ) (取自这里

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


This might also help solve the confusion between ExitStatus/BatchStatus. 也可能有助于解决 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.关于计时阈值...据我所知,到目前为止,您计划测量当前步骤的时间(beforestep - afterstep),无论之前的步骤运行了多长时间。 To calculate and the previous steps try adding a timestamp in JobParameters and check when that threshold is passed.要计算和前面的步骤,请尝试在 JobParameters 中添加时间戳并检查何时超过该阈值。

See this question right here , the OP has the solution to this in his question. 在这里看到这个问题,OP 在他的问题中有解决方案。

The jobListener is a little bit different jobListener有点不同

@Override
public void beforeJob(JobExecution jobExecution) {
        JobParameters jobParameters = jobExecution.getJobParameters();
        
        //check params etc
        
        if (!shouldExecute) {
            jobExecution.stop();
            jobExecution.setExitStatus(new ExitStatus("STOPPED", "Invalid state."));
            return;
        }
}

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.如果您希望即使在最后一步广播时间警报也能正常完成作业,您可以检查(使用JobOperator )以查询作业进度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM