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

Time:11-12

I'm using spring.batch.core's JobExecution. And I'm using JobExecutionListenerSupport's overridden method afterJob like this

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

I want to know when does jobExecution.getStatus() == BatchStatus.STOPPED become true?

CodePudding user response:

The Spring Docs explain when a job will be STOPPED

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.

CodePudding user response:

This blog also helps in answering it. [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.

  • Related