jobExecution.getStatus() return 什么时候在 java 中有一个 BatchStatus.STOPPED 值?

When does jobExecution.getStatus() return a BatchStatus.STOPPED value in java?

我正在使用 spring.batch.core 的 JobExecution。我正在像这样使用 JobExecutionListenerSupport 的覆盖方法 afterJob

@Override
 public void afterJob(JobExecution jobExecution) {
        if (jobExecution.getStatus() == BatchStatus.FAILED) {
            
            log.error();
            throw new AbortingJobException();
        } else if (jobExecution.getStatus() == BatchStatus.STOPPED) {
            log.info(jobExecution.getJobInstance().getJobName() + " Stopped");
        } else if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
            // does something
        }
    }

我想知道jobExecution.getStatus() == BatchStatus.STOPPED什么时候变成真的?

Spring 文档解释了作业何时停止

https://docs.spring.io/spring-batch/docs/current/reference/html/job.html#stoppingAJob

The shutdown is not immediate, since there is no way to force immediate shutdown, especially if the execution is currently in developer code that the framework has no control over, such as a business service. However, as soon as control is returned back to the framework, it will set the status of the current StepExecution to BatchStatus.STOPPED, save it, then do the same for the JobExecution before finishing.

这篇博文也有助于解答。 [https://blog.codecentric.de/en/2014/04/spring-batch-batchstatus-state-transitions/]

The BatchStatus of the job is set to STOPPING, and steps will check for this state at chunk boundaries to stop themselves. When all steps are stopped, the state of the job is set to STOPPED as well. This means that if a step is looping inside a chunk, or if the server is shut down during stopping the batch, the job will always stay in state STOPPING.