From 5f070affb7e4276bf83fe33e3e33af0eacb95d67 Mon Sep 17 00:00:00 2001 From: robokaso Date: Mon, 6 Oct 2008 07:45:19 +0000 Subject: [PATCH] RESOLVED - BATCH-825: Modify JobLauncher contract to not throw exception on job failure. removed onInterrupt from JobExecutionListener --- .../batch/core/JobExecutionListener.java | 5 --- .../batch/core/job/SimpleJob.java | 31 ++++++++++--------- .../CompositeExecutionJobListener.java | 12 ------- .../listener/JobExecutionListenerSupport.java | 6 ---- .../batch/core/job/SimpleJobTests.java | 2 +- 5 files changed, 17 insertions(+), 39 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecutionListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecutionListener.java index ba1f730e2..2619a2542 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecutionListener.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/JobExecutionListener.java @@ -39,9 +39,4 @@ public interface JobExecutionListener { */ void afterJob(JobExecution jobExecution); - /** - * Callback when a job is interrupted or stopped manually. - * @param jobExecution the current {@link JobExecution} - */ - void onInterrupt(JobExecution jobExecution); } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java index 352be91f0..91cb70a89 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java @@ -19,6 +19,8 @@ package org.springframework.batch.core.job; import java.util.Date; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; @@ -41,6 +43,8 @@ import org.springframework.batch.repeat.ExitStatus; */ public class SimpleJob extends AbstractJob { + private static final Log logger = LogFactory.getLog(SimpleJob.class); + /** * Run the specified job by looping through the steps and delegating to the * {@link Step}. @@ -49,7 +53,7 @@ public class SimpleJob extends AbstractJob { * @throws StartLimitExceededException if start limit of one of the steps * was exceeded */ - public void execute(JobExecution execution){ + public void execute(JobExecution execution) { JobInstance jobInstance = execution.getJobInstance(); @@ -82,7 +86,8 @@ public class SimpleJob extends AbstractJob { updateStatus(execution, BatchStatus.STARTED); currentStepExecution = execution.createStepExecution(step.getName()); - StepExecution lastStepExecution = getJobRepository().getLastStepExecution(jobInstance, step.getName()); + StepExecution lastStepExecution = getJobRepository().getLastStepExecution(jobInstance, + step.getName()); boolean isRestart = (lastStepExecution != null && !lastStepExecution.getStatus().equals( BatchStatus.COMPLETED)) ? true : false; @@ -96,8 +101,8 @@ public class SimpleJob extends AbstractJob { step.execute(currentStepExecution); - if(currentStepExecution.getStatus() == BatchStatus.FAILED || - currentStepExecution.getStatus() == BatchStatus.STOPPED ){ + if (currentStepExecution.getStatus() == BatchStatus.FAILED + || currentStepExecution.getStatus() == BatchStatus.STOPPED) { break; } } @@ -110,25 +115,14 @@ public class SimpleJob extends AbstractJob { updateStatus(execution, currentStepExecution.getStatus()); - //This is temporary, given the changes to how exceptions are handled, there really should only be an afterJob method - //but it's being left as is until the listener contract can be discussed. - if(execution.getStatus() != BatchStatus.STOPPED){ - getCompositeListener().afterJob(execution); - } - else if(execution.getStatus() == BatchStatus.STOPPED){ - getCompositeListener().onInterrupt(execution); - } - } catch (JobInterruptedException e) { execution.setStatus(BatchStatus.STOPPED); execution.addFailureException(e); - getCompositeListener().onInterrupt(execution); } catch (Throwable t) { execution.setStatus(BatchStatus.FAILED); execution.addFailureException(t); - getCompositeListener().afterJob(execution); } finally { ExitStatus status = ExitStatus.FAILED; @@ -148,6 +142,13 @@ public class SimpleJob extends AbstractJob { execution.setEndTime(new Date()); execution.setExitStatus(status); getJobRepository().update(execution); + + try { + getCompositeListener().afterJob(execution); + } + catch (Exception e) { + logger.error("Exception encountered in afterStep callback", e); + } } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/CompositeExecutionJobListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/CompositeExecutionJobListener.java index c786152ad..76e87ff68 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/CompositeExecutionJobListener.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/CompositeExecutionJobListener.java @@ -72,16 +72,4 @@ public class CompositeExecutionJobListener implements JobExecutionListener { } } - /** - * Call the registered listeners in reverse order, respecting and - * prioritising those that implement {@link Ordered}. - * @see org.springframework.batch.core.JobExecutionListener#onInterrupt(org.springframework.batch.core.JobExecution) - */ - public void onInterrupt(JobExecution jobExecution) { - for (Iterator iterator = listeners.reverse(); iterator.hasNext();) { - JobExecutionListener listener = iterator.next(); - listener.onInterrupt(jobExecution); - } - - } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/JobExecutionListenerSupport.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/JobExecutionListenerSupport.java index 67540a16d..9f793c5f2 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/JobExecutionListenerSupport.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/JobExecutionListenerSupport.java @@ -36,10 +36,4 @@ public class JobExecutionListenerSupport implements JobExecutionListener { public void beforeJob(JobExecution jobExecution) { } - /* (non-Javadoc) - * @see org.springframework.batch.core.JobListener#onInterrupt(org.springframework.batch.core.JobExecution) - */ - public void onInterrupt(JobExecution jobExecution) { - } - } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java index fbed4e876..d8610a28b 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java @@ -334,7 +334,7 @@ public class SimpleJobTests extends TestCase { JobExecutionListener listener = createMock(JobExecutionListener.class); listener.beforeJob(jobExecution); - listener.onInterrupt(jobExecution); + listener.afterJob(jobExecution); replay(listener); job.setJobExecutionListeners(new JobExecutionListener[] { listener });