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?

No correct solution

OTHER TIPS

See this question right here, the OP has the solution to this in his question.

The jobListener is a little bit different

@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;
        }
}

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.

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.

i suggest looking at CompletionPolicies especially the TimeoutTerminationPolicy

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top