Merge pull request #49 from chrisjs/BATCH-1666

BATCH-1666: Add abort(long executionId) convenience method to JobOperato...
This commit is contained in:
Michael Minella
2012-10-02 12:23:34 -07:00
3 changed files with 56 additions and 1 deletions

View File

@@ -197,4 +197,17 @@ public interface JobOperator {
*/
Set<String> getJobNames();
/**
* Mark the {@link JobExecution} as ABANDONED. If a stop signal is ignored
* because the process died this is the best way to mark a job as finished
* with (as opposed to STOPPED). An abandoned job execution can be
* restarted, but a stopping one cannot.
*
* @param jobExecutionId the job execution id to abort
* @return the {@link JobExecution} that was aborted
* @throws NoSuchJobExecutionException
* @throws JobExecutionAlreadyRunningException if the job is running (it
* should be stopped first)
*/
JobExecution abandon(long jobExecutionId) throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException;
}

View File

@@ -16,6 +16,7 @@
package org.springframework.batch.core.launch.support;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -382,6 +383,22 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
return true;
}
public JobExecution abandon(long jobExecutionId) throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException {
JobExecution jobExecution = findExecutionById(jobExecutionId);
if (jobExecution.getStatus().isLessThan(BatchStatus.STOPPING)) {
throw new JobExecutionAlreadyRunningException(
"JobExecution is running or complete and therefore cannot be aborted");
}
logger.info("Aborting job execution: " + jobExecution);
jobExecution.upgradeStatus(BatchStatus.ABANDONED);
jobExecution.setEndTime(new Date());
jobRepository.update(jobExecution);
return jobExecution;
}
private JobExecution findExecutionById(long executionId) throws NoSuchJobExecutionException {
JobExecution jobExecution = jobExplorer.getJobExecution(executionId);
@@ -391,5 +408,4 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
return jobExecution;
}
}

View File

@@ -20,6 +20,7 @@ import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -393,4 +394,29 @@ public class SimpleJobOperatorTests {
assertEquals(BatchStatus.STOPPING, jobExecution.getStatus());
}
@Test
public void testAbort() throws Exception {
JobInstance jobInstance = new JobInstance(123L, jobParameters, job.getName());
JobExecution jobExecution = new JobExecution(jobInstance, 111L);
jobExecution.setStatus(BatchStatus.STOPPING);
jobExplorer.getJobExecution(123L);
expectLastCall().andReturn(jobExecution);
jobRepository.update(jobExecution);
replay(jobExplorer);
jobOperator.abandon(123L);
assertEquals(BatchStatus.ABANDONED, jobExecution.getStatus());
assertNotNull(jobExecution.getEndTime());
}
@Test(expected = JobExecutionAlreadyRunningException.class)
public void testAbortNonStopping() throws Exception {
JobInstance jobInstance = new JobInstance(123L, jobParameters, job.getName());
JobExecution jobExecution = new JobExecution(jobInstance, 111L);
jobExecution.setStatus(BatchStatus.STARTED);
jobExplorer.getJobExecution(123L);
expectLastCall().andReturn(jobExecution);
jobRepository.update(jobExecution);
replay(jobExplorer);
jobOperator.abandon(123L);
}
}