Ensure job interruption is treated as exception in step and job

This commit is contained in:
dsyer
2008-07-07 14:20:59 +00:00
parent 3468859411
commit 057aa351a5
4 changed files with 50 additions and 1 deletions

View File

@@ -102,6 +102,11 @@ public class SimpleJob extends AbstractJob {
}
}
// Need to check again for stopped job
if (execution.getStatus() == BatchStatus.STOPPING) {
throw new JobInterruptedException("JobExecution interrupted.");
}
updateStatus(execution, BatchStatus.COMPLETED);
getCompositeListener().afterJob(execution);

View File

@@ -169,6 +169,12 @@ public abstract class AbstractStep implements Step, InitializingBean, BeanNameAw
}
exitStatus = doExecute(stepExecution);
// Check if someone is trying to stop us
if (stepExecution.isTerminateOnly()) {
stepExecution.setStatus(BatchStatus.STOPPED);
throw new JobInterruptedException("JobExecution interrupted.");
}
stepExecution.setStatus(BatchStatus.COMPLETED);
exitStatus = exitStatus.and(getCompositeListener().afterStep(stepExecution));

View File

@@ -7,6 +7,7 @@ import junit.framework.TestCase;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
@@ -175,6 +176,43 @@ public class AbstractStepTests extends TestCase {
.containsKey("onErrorInStep"));
}
/**
* Exception during business processing.
*/
public void testStoppedStep() throws Exception {
tested = new EventTrackingStep() {
protected ExitStatus doExecute(StepExecution stepExecution) throws Exception {
stepExecution.setTerminateOnly();
return super.doExecute(stepExecution);
}
};
tested.setJobRepository(repository);
tested.setStepExecutionListeners(new StepExecutionListener[] { listener1, listener2 });
try {
tested.execute(execution);
fail();
}
catch (JobInterruptedException expected) {
assertEquals("JobExecution interrupted.", expected.getMessage());
}
int i = 0;
assertEquals("listener1#beforeStep", events.get(i++));
assertEquals("listener2#beforeStep", events.get(i++));
assertEquals("open", events.get(i++));
assertEquals("doExecute", events.get(i++));
assertEquals("listener2#onErrorInStep", events.get(i++));
assertEquals("listener1#onErrorInStep", events.get(i++));
assertEquals("close", events.get(i++));
assertEquals(7, events.size());
assertEquals("JOB_INTERRUPTED", execution.getExitStatus().getExitCode());
assertTrue("Execution context modifications made by listener should be persisted", repository.saved
.containsKey("onErrorInStep"));
}
/**
* Exception during business processing.
*/