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.
*/

View File

@@ -12,5 +12,5 @@
<stringAttribute key="org.eclipse.debug.core.source_locator_memento" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;sourceLookupDirector&gt;&#13;&#10;&lt;sourceContainers duplicates=&quot;false&quot;&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-core&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-execution&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-infrastructure&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-integration&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;javaProject name=&amp;quot;spring-batch-samples&amp;quot;/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.jdt.launching.sourceContainer.javaProject&quot;/&gt;&#13;&#10;&lt;container memento=&quot;&amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&amp;gt;&amp;#13;&amp;#10;&amp;lt;default/&amp;gt;&amp;#13;&amp;#10;&quot; typeId=&quot;org.eclipse.debug.core.containerType.default&quot;/&gt;&#13;&#10;&lt;/sourceContainers&gt;&#13;&#10;&lt;/sourceLookupDirector&gt;&#13;&#10;"/>
<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>