RESOLVED - BATCH-972: Job has the same ExitStatus as the last StepExecution

moved JobListener calls after JobExecution's ExitStatus has been set
This commit is contained in:
robokaso
2009-01-05 12:18:18 +00:00
parent da4cf938dd
commit 03904e018a
2 changed files with 27 additions and 7 deletions

View File

@@ -58,6 +58,9 @@ public class SimpleJob extends AbstractJob {
int startedCount = 0;
List steps = getSteps();
// holder for potential job failure to be passed to onError listener
Throwable jobFailure = null;
try {
// The job was already stopped before we even got this far. Deal
@@ -109,17 +112,14 @@ public class SimpleJob extends AbstractJob {
updateStatus(execution, BatchStatus.COMPLETED);
getCompositeListener().afterJob(execution);
}
catch (JobInterruptedException e) {
execution.setStatus(BatchStatus.STOPPED);
getCompositeListener().onInterrupt(execution);
rethrow(e);
}
catch (Throwable t) {
execution.setStatus(BatchStatus.FAILED);
getCompositeListener().onError(execution, t);
jobFailure = t;
rethrow(t);
}
finally {
@@ -136,9 +136,20 @@ public class SimpleJob extends AbstractJob {
else if (currentStepExecution != null) {
status = currentStepExecution.getExitStatus();
}
execution.setExitStatus(status);
BatchStatus jobStatus = execution.getStatus();
if (jobStatus == BatchStatus.COMPLETED) {
getCompositeListener().afterJob(execution);
}
else if (jobStatus == BatchStatus.FAILED) {
getCompositeListener().onError(execution, jobFailure);
}
else if (jobStatus == BatchStatus.STOPPED) {
getCompositeListener().onInterrupt(execution);
}
execution.setEndTime(new Date());
execution.setExitStatus(status);
getJobRepository().saveOrUpdate(execution);
}

View File

@@ -81,6 +81,8 @@ public class SimpleJobTests extends TestCase {
private SimpleJob job;
private static final ExitStatus customExitStatus = ExitStatus.UNKNOWN.addExitDescription("tweaked");
protected void setUp() throws Exception {
super.setUp();
@@ -178,10 +180,13 @@ public class SimpleJobTests extends TestCase {
public void afterJob(JobExecution jobExecution) {
list.add("after");
jobExecution.setExitStatus(customExitStatus);
}
} });
job.execute(jobExecution);
assertEquals(4, list.size());
assertEquals(customExitStatus, jobExecution.getExitStatus());
checkRepository(BatchStatus.COMPLETED, customExitStatus);
}
public void testRunWithSimpleStepExecutor() throws Exception {
@@ -240,6 +245,8 @@ public class SimpleJobTests extends TestCase {
job.setJobExecutionListeners(new JobExecutionListenerSupport[] { new JobExecutionListenerSupport() {
public void onError(JobExecution jobExecution, Throwable t) {
list.add(t);
assertEquals(ExitStatus.FAILED, jobExecution.getExitStatus());
jobExecution.setExitStatus(customExitStatus);
}
} });
final RuntimeException exception = new RuntimeException("Foo!");
@@ -254,7 +261,7 @@ public class SimpleJobTests extends TestCase {
}
assertEquals(1, list.size());
assertSame(exception, list.get(0));
checkRepository(BatchStatus.FAILED, ExitStatus.FAILED);
checkRepository(BatchStatus.FAILED, customExitStatus);
}
public void testFailedWithError() throws Exception {
@@ -489,7 +496,9 @@ public class SimpleJobTests extends TestCase {
/*
* (non-Javadoc)
* @see org.springframework.batch.core.step.StepSupport#execute(org.springframework.batch.core.StepExecution)
*
* @seeorg.springframework.batch.core.step.StepSupport#execute(org.
* springframework.batch.core.StepExecution)
*/
public void execute(StepExecution stepExecution) throws JobInterruptedException,
UnexpectedJobExecutionException {