diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java b/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java index bbe83c80ca..cabfbb0852 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java @@ -259,6 +259,14 @@ public StepExecution createStepExecution(String stepName) { return stepExecution; } + /** + * Test if this {@link JobExecution} indicates that it is waiting to start. + * @return true if the status is {@link BatchStatus#STARTING}. + */ + public boolean isStarting() { + return status == BatchStatus.STARTING; + } + /** * Test if this {@link JobExecution} indicates that it is running. * Note that this does not necessarily mean that it has been persisted. 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 6b8ebf037a..1de26969f2 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 @@ -123,7 +123,7 @@ public JobExecution createJobExecution(String jobName, JobParameters jobParamete // check for running executions and find the last started for (JobExecution execution : executions) { - if (execution.isRunning() || execution.isStopping()) { + if (execution.isStarting() || execution.isRunning() || execution.isStopping()) { throw new JobExecutionAlreadyRunningException("A job execution for this job is already running: " + jobInstance); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java index e2204bcd8d..295b6e883a 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/repository/support/SimpleJobRepositoryIntegrationTests.java @@ -174,6 +174,25 @@ public void testSaveExecutionContext() throws Exception { // assertEquals(ctx, retrievedJobExec.getExecutionContext()); } + /* + * If JobExecution is waiting to start, exception will be thrown in attempt + * to create new execution. + */ + @Transactional + @Test + public void testStartingJobRejectsAnotherJobExecution() throws Exception { + job.setRestartable(true); + JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters); + + try { + jobRepository.createJobExecution(job.getName(), jobParameters); + fail(); + } + catch (JobExecutionAlreadyRunningException e) { + // expected + } + } + /* * If JobExecution is already running, exception will be thrown in attempt * to create new execution.