For JSR-352, removed the restriction of not being able to abnadon a completed job

This commit is contained in:
Michael Minella
2013-08-23 14:00:59 -05:00
parent 2a3bb817c0
commit 3947a0ef49
2 changed files with 23 additions and 12 deletions

View File

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

View File

@@ -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<JobExecution> 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);
}