From 3947a0ef49cd820f01dcaef59b23102f62cc2da8 Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Fri, 23 Aug 2013 14:00:59 -0500 Subject: [PATCH] For JSR-352, removed the restriction of not being able to abnadon a completed job --- .../batch/core/jsr/launch/JsrJobOperator.java | 18 +++++++++++------- .../core/jsr/launch/JsrJobOperatorTests.java | 17 ++++++++++++----- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java index c3c6f1877..4c609a388 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/launch/JsrJobOperator.java @@ -47,7 +47,6 @@ import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.jsr.JobContext; import org.springframework.batch.core.jsr.JsrJobParametersConverter; import org.springframework.batch.core.jsr.configuration.support.BatchPropertyContext; -import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; import org.springframework.batch.core.repository.JobRepository; import org.springframework.beans.factory.access.BeanFactoryLocator; import org.springframework.beans.factory.access.BeanFactoryReference; @@ -203,13 +202,18 @@ public class JsrJobOperator implements JobOperator { @Override public void abandon(long jobExecutionId) throws NoSuchJobExecutionException, JobExecutionIsRunningException, JobSecurityException { - try { - batchJobOperator.abandon(jobExecutionId); - } catch (org.springframework.batch.core.launch.NoSuchJobExecutionException e) { - throw new NoSuchJobExecutionException(e); - } catch (JobExecutionAlreadyRunningException e) { - throw new JobExecutionIsRunningException(e); + org.springframework.batch.core.JobExecution jobExecution = jobExplorer.getJobExecution(jobExecutionId); + + if(jobExecution == null) { + throw new NoSuchJobExecutionException("Unable to retrieve JobExecution for id " + jobExecutionId); } + + if(jobExecution.isRunning()) { + throw new JobExecutionIsRunningException("Unable to abandon a job that is currently running"); + } + + jobExecution.upgradeStatus(BatchStatus.ABANDONED); + jobRepository.update(jobExecution); } /* (non-Javadoc) diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java index 18335e40c..36a9aa885 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/launch/JsrJobOperatorTests.java @@ -9,6 +9,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.ArrayList; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Properties; @@ -24,6 +25,7 @@ import javax.batch.runtime.BatchStatus; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.batch.core.JobExecution; @@ -36,7 +38,6 @@ import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.explore.support.SimpleJobExplorer; import org.springframework.batch.core.jsr.JsrJobParametersConverter; import org.springframework.batch.core.launch.support.SimpleJobOperator; -import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.JobRepositorySupport; @@ -95,21 +96,27 @@ public class JsrJobOperatorTests { @Test public void testAbandonRoseyScenario() throws Exception { + JobExecution jobExecution = new JobExecution(5l); + jobExecution.setEndTime(new Date()); + when(jobExplorer.getJobExecution(5l)).thenReturn(jobExecution); + jsrJobOperator.abandon(5l); - verify(jobOperator).abandon(5l); + ArgumentCaptor executionCaptor = ArgumentCaptor.forClass(JobExecution.class); + verify(jobRepository).update(executionCaptor.capture()); + assertEquals(org.springframework.batch.core.BatchStatus.ABANDONED, executionCaptor.getValue().getStatus()); + } @Test(expected=NoSuchJobExecutionException.class) public void testAbandonNoSuchJob() throws Exception { - when(jobOperator.abandon(5l)).thenThrow(new org.springframework.batch.core.launch.NoSuchJobExecutionException("expected")); - jsrJobOperator.abandon(5l); } @Test(expected=JobExecutionIsRunningException.class) public void testAbandonJobRunning() throws Exception { - when(jobOperator.abandon(5l)).thenThrow(new JobExecutionAlreadyRunningException("expected")); + JobExecution jobExecution = new JobExecution(5l); + when(jobExplorer.getJobExecution(5l)).thenReturn(jobExecution); jsrJobOperator.abandon(5l); }