diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java index f3abe6e81..8f29f9cc6 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobLauncher.java @@ -17,6 +17,8 @@ package org.springframework.batch.core.launch.support; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobInstance; @@ -30,6 +32,7 @@ import org.springframework.batch.core.repository.JobRestartException; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.task.SyncTaskExecutor; import org.springframework.core.task.TaskExecutor; +import org.springframework.core.task.TaskRejectedException; import org.springframework.util.Assert; /** @@ -105,29 +108,40 @@ public class SimpleJobLauncher implements JobLauncher, InitializingBean { jobExecution = jobRepository.createJobExecution(job.getName(), jobParameters); } - taskExecutor.execute(new Runnable() { + try { + taskExecutor.execute(new Runnable() { - public void run() { - try { - logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters + "]"); - job.execute(jobExecution); - logger.info("Job: [" + job + "] completed with the following parameters: [" + jobParameters - + "] and the following status: [" + jobExecution.getStatus() + "]"); + public void run() { + try { + logger.info("Job: [" + job + "] launched with the following parameters: [" + jobParameters + + "]"); + job.execute(jobExecution); + logger.info("Job: [" + job + "] completed with the following parameters: [" + jobParameters + + "] and the following status: [" + jobExecution.getStatus() + "]"); + } + catch (Throwable t) { + logger.info("Job: [" + job + + "] failed unexpectedly and fatally with the following parameters: [" + jobParameters + + "]", t); + rethrow(t); + } } - catch (Throwable t) { - logger.info("Job: [" + job + "] failed unexpectedly and fatally with the following parameters: [" - + jobParameters + "]", t); - rethrow(t); - } - } - private void rethrow(Throwable t) { - if (t instanceof RuntimeException) { - throw (RuntimeException) t; + private void rethrow(Throwable t) { + if (t instanceof RuntimeException) { + throw (RuntimeException) t; + } + throw new RuntimeException(t); } - throw new RuntimeException(t); + }); + } + catch (TaskRejectedException e) { + jobExecution.upgradeStatus(BatchStatus.FAILED); + if (jobExecution.getExitStatus().equals(ExitStatus.UNKNOWN)) { + jobExecution.setExitStatus(ExitStatus.FAILED.addExitDescription(e)); } - }); + jobRepository.update(jobExecution); + } return jobExecution; } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/SimpleJobLauncherTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/SimpleJobLauncherTests.java index fa5eda747..aa45a01be 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/SimpleJobLauncherTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/SimpleJobLauncherTests.java @@ -18,6 +18,7 @@ package org.springframework.batch.core.launch; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.expectLastCall; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.reset; import static org.easymock.EasyMock.verify; @@ -30,6 +31,7 @@ import java.util.List; import org.junit.Before; import org.junit.Test; +import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; @@ -40,6 +42,7 @@ import org.springframework.batch.core.launch.support.SimpleJobLauncher; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.JobRestartException; import org.springframework.core.task.TaskExecutor; +import org.springframework.core.task.TaskRejectedException; /** * @author Lucas Ward @@ -139,6 +142,39 @@ public class SimpleJobLauncherTests { assertEquals(1, list.size()); } + @Test + public void testTaskExecutorRejects() throws Exception { + + final List list = new ArrayList(); + jobLauncher.setTaskExecutor(new TaskExecutor() { + public void execute(Runnable task) { + list.add("execute"); + throw new TaskRejectedException("Planned failure"); + } + }); + + JobExecution jobExecution = new JobExecution(null, null); + + expect(jobRepository.getLastJobExecution(job.getName(), jobParameters)).andReturn(null); + expect(jobRepository.createJobExecution(job.getName(), jobParameters)).andReturn(jobExecution); + jobRepository.update(jobExecution); + expectLastCall(); + replay(jobRepository); + + jobLauncher.afterPropertiesSet(); + try { + jobLauncher.run(job, jobParameters); + } + finally { + assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); + assertEquals(ExitStatus.FAILED.getExitCode(), jobExecution.getExitStatus().getExitCode()); + verify(jobRepository); + } + + assertEquals(1, list.size()); + + } + @Test public void testRunWithException() throws Exception { job = new JobSupport() {