diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java index c5743ea2c5..e962b426d9 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/SimpleJobRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2021 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -172,6 +172,12 @@ public void update(JobExecution jobExecution) { jobExecution.setLastUpdated(new Date(System.currentTimeMillis())); jobExecutionDao.synchronizeStatus(jobExecution); + if (jobExecution.getStatus() == BatchStatus.STOPPING && jobExecution.getEndTime() != null) { + if (logger.isInfoEnabled()) { + logger.info("Upgrading job execution status from STOPPING to STOPPED since it has already ended."); + } + jobExecution.upgradeStatus(BatchStatus.STOPPED); + } jobExecutionDao.updateJobExecution(jobExecution); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java index 0609c43e7a..b3bb5d4ca6 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2020 the original author or authors. + * Copyright 2006-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -295,4 +295,15 @@ public void testGetStepExecutionCount() { // Then assertEquals(expectedResult, actualResult); } + + @Test + public void testUpgradeStopping() { + jobExecution.setStatus(BatchStatus.STOPPING); + jobExecution.setEndTime(new Date()); + + jobRepository.update(jobExecution); + + assertEquals(BatchStatus.STOPPED, jobExecution.getStatus()); + } + }