Ensure job interruption is treated as exception in step and job
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -12,5 +12,5 @@
|
||||
<stringAttribute key="org.eclipse.debug.core.source_locator_memento" value="<?xml version="1.0" encoding="UTF-8"?> <sourceLookupDirector> <sourceContainers duplicates="false"> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-core&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-execution&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-infrastructure&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-integration&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;javaProject name=&quot;spring-batch-samples&quot;/&gt;&#13;&#10;" typeId="org.eclipse.jdt.launching.sourceContainer.javaProject"/> <container memento="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;default/&gt;&#13;&#10;" typeId="org.eclipse.debug.core.containerType.default"/> </sourceContainers> </sourceLookupDirector> "/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="org.springframework.batch.sample.launch.TaskExecutorLauncher"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="spring-batch-samples"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dcom.sun.management.jmxremote -Dplayer.file.name=player.csv -Dgames.file.name=games.csv -Dbatch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples"/>
|
||||
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-Dcom.sun.management.jmxremote -Dplayer.file.name=player.csv -Dgames.file.name=games.csv -Djob.commit.interval=50 -Dbatch.jdbc.url=jdbc:hsqldb:hsql://localhost:9005/samples"/>
|
||||
</launchConfiguration>
|
||||
|
||||
Reference in New Issue
Block a user