diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java index db05b3256..d808e2d00 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java @@ -232,16 +232,27 @@ public abstract class AbstractJob implements Job, BeanNameAware, InitializingBea // The job was already stopped before we even got this far. Deal // with it in the same way as any other interruption. - execution.setStatus(BatchStatus.STOPPED); - execution.setExitStatus(ExitStatus.FINISHED); + if (execution.getStatus() == BatchStatus.PAUSED) { + // do nothing + } + else { + + execution.setStatus(BatchStatus.STOPPED); + execution.setExitStatus(ExitStatus.FINISHED); + } } } catch (JobInterruptedException e) { logger.error(e); - execution.setExitStatus(ExitStatus.FAILED); - execution.setStatus(BatchStatus.STOPPED); - execution.addFailureException(e); + if (execution.getStatus() == BatchStatus.PAUSED) { + // do nothing + } + else { + execution.setExitStatus(ExitStatus.FAILED); + execution.setStatus(BatchStatus.STOPPED); + execution.addFailureException(e); + } } catch (Throwable t) { logger.error(t); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java index 191b3f6f8..fe90338c4 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java @@ -87,6 +87,8 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean { if (lastExecution != null) { if (lastExecution.getStatus() == BatchStatus.PAUSED) { jobExecution = lastExecution; + // this execution will be continued => delete the end time + jobExecution.setEndTime(null); } else if (!job.isRestartable()) { throw new JobRestartException("JobInstance already exists and is not restartable"); 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 26ef998bb..cd17d1ee9 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 @@ -320,8 +320,9 @@ public class SimpleJobRepository implements JobRepository { * @param stepExecution */ private void checkForInterruption(StepExecution stepExecution){ - jobExecutionDao.synchronizeStatus(stepExecution.getJobExecution()); - if(stepExecution.getJobExecution().getStatus() == BatchStatus.STOPPING){ + JobExecution jobExecution = stepExecution.getJobExecution(); + jobExecutionDao.synchronizeStatus(jobExecution); + if(jobExecution.getStatus() == BatchStatus.STOPPING || jobExecution.getStatus() == BatchStatus.PAUSED){ stepExecution.setTerminateOnly(); } } diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/launch/RemoteLauncherTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/launch/RemoteLauncherTests.java index c901b12fb..cb67828fd 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/launch/RemoteLauncherTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/launch/RemoteLauncherTests.java @@ -93,17 +93,24 @@ public class RemoteLauncherTests { // sleep long enough to avoid race conditions (serializable tx isolation // doesn't work with HSQL) Thread.sleep(SLEEP_INTERVAL); +// assertEquals(1, launcher.getRunningExecutions("loopJob").size()); launcher.pause(executionId); Thread.sleep(SLEEP_INTERVAL); +// assertEquals(0, launcher.getRunningExecutions("loopJob").size()); + logger.debug(launcher.getSummary(executionId)); long resumedId = launcher.resume(executionId); assertEquals("Picked up the same execution after pause and resume", executionId, resumedId); - -// launcher.pause(executionId); -// Thread.sleep(SLEEP_INTERVAL); -// long resumeId2 = launcher.resume(executionId); -// assertEquals("Picked up the same execution after pause and resume", executionId, resumeId2); - + + Thread.sleep(SLEEP_INTERVAL); + launcher.pause(executionId); + Thread.sleep(SLEEP_INTERVAL); + +// assertEquals(0, launcher.getRunningExecutions("loopJob").size()); + logger.debug(launcher.getSummary(executionId)); + long resumeId2 = launcher.resume(executionId); + assertEquals("Picked up the same execution after pause and resume", executionId, resumeId2); + launcher.stop(executionId); }