From dbf9336e60757f5906b88439aa212a643ead589c Mon Sep 17 00:00:00 2001 From: dsyer Date: Wed, 14 Nov 2007 08:48:08 +0000 Subject: [PATCH] Change logging level of error in RepeatTemplate --- .../simple/SimpleStepExecutorFactory.java | 28 ++++++-- .../SimpleStepExecutorFactoryTests.java | 66 ++++++++++++++++--- .../batch/repeat/support/RepeatTemplate.java | 3 +- 3 files changed, 82 insertions(+), 15 deletions(-) diff --git a/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactory.java b/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactory.java index e4d3e327a..a78a4cadf 100644 --- a/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactory.java +++ b/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactory.java @@ -19,6 +19,7 @@ import org.springframework.batch.core.configuration.StepConfiguration; import org.springframework.batch.core.executor.StepExecutor; import org.springframework.batch.core.executor.StepExecutorFactory; import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.execution.step.RepeatOperationsHolder; import org.springframework.batch.execution.step.SimpleStepConfiguration; import org.springframework.batch.repeat.RepeatOperations; import org.springframework.batch.repeat.exception.handler.ExceptionHandler; @@ -62,14 +63,29 @@ public class SimpleStepExecutorFactory implements StepExecutorFactory, SimpleStepExecutor executor = new SimpleStepExecutor(); executor.setRepository(jobRepository); + RepeatTemplate template = new RepeatTemplate(); RepeatOperations repeatOperations = template; - SimpleStepConfiguration simpleConfiguration = (SimpleStepConfiguration) configuration; - template.setCompletionPolicy(new SimpleCompletionPolicy( - simpleConfiguration.getCommitInterval())); - ExceptionHandler exceptionHandler = simpleConfiguration.getExceptionHandler(); - if (exceptionHandler!=null) { - template.setExceptionHandler(exceptionHandler); + + if (configuration instanceof RepeatOperationsHolder) { + + repeatOperations = ((RepeatOperationsHolder) configuration) + .getChunkOperations(); + Assert + .state(repeatOperations != null, + "Chunk operations obtained from step configuration must be non-null."); + + } else { + + SimpleStepConfiguration simpleConfiguration = (SimpleStepConfiguration) configuration; + template.setCompletionPolicy(new SimpleCompletionPolicy( + simpleConfiguration.getCommitInterval())); + ExceptionHandler exceptionHandler = simpleConfiguration + .getExceptionHandler(); + if (exceptionHandler != null) { + template.setExceptionHandler(exceptionHandler); + } + } executor.setChunkOperations(repeatOperations); diff --git a/execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactoryTests.java b/execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactoryTests.java index 745a44244..287d38ac1 100644 --- a/execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactoryTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/step/simple/SimpleStepExecutorFactoryTests.java @@ -26,9 +26,14 @@ import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.core.domain.StepInstance; +import org.springframework.batch.execution.step.RepeatOperationsHolder; import org.springframework.batch.execution.step.SimpleStepConfiguration; import org.springframework.batch.repeat.RepeatContext; +import org.springframework.batch.repeat.RepeatOperations; import org.springframework.batch.repeat.exception.handler.ExceptionHandler; +import org.springframework.batch.repeat.interceptor.RepeatInterceptorAdapter; +import org.springframework.batch.repeat.policy.SimpleCompletionPolicy; +import org.springframework.batch.repeat.support.RepeatTemplate; /** * @author Dave Syer @@ -41,7 +46,7 @@ public class SimpleStepExecutorFactoryTests extends TestCase { protected void setUp() throws Exception { factory.setJobRepository(new JobRepositorySupport()); } - + public void testSuccessfulStepExecutor() throws Exception { assertNotNull(factory.getExecutor(new SimpleStepConfiguration())); } @@ -56,8 +61,11 @@ public class SimpleStepExecutorFactoryTests extends TestCase { throw new RuntimeException("Oops"); } }); - SimpleStepExecutor executor = (SimpleStepExecutor) factory.getExecutor(configuration); - StepExecution stepExecution = new StepExecution(new StepInstance(new Long(11)), new JobExecution(new JobInstance(null), new Long(12))); + SimpleStepExecutor executor = (SimpleStepExecutor) factory + .getExecutor(configuration); + StepExecution stepExecution = new StepExecution(new StepInstance( + new Long(11)), new JobExecution(new JobInstance(null), + new Long(12))); try { executor.processChunk(configuration, stepExecution); fail("Expected RuntimeException"); @@ -67,6 +75,31 @@ public class SimpleStepExecutorFactoryTests extends TestCase { assertEquals(1, list.size()); } + public void testSuccessfulRepeatOperationsHolder() throws Exception { + RepeatTemplate repeatTemplate = new RepeatTemplate(); + final List list = new ArrayList(); + repeatTemplate.setInterceptor(new RepeatInterceptorAdapter() { + public void onError(RepeatContext context, Throwable e) { + list.add(e); + } + }); + repeatTemplate.setCompletionPolicy(new SimpleCompletionPolicy(2)); + SimpleHolderStepConfiguration configuration = new SimpleHolderStepConfiguration( + repeatTemplate); + SimpleStepExecutor executor = (SimpleStepExecutor) factory + .getExecutor(configuration); + StepExecution stepExecution = new StepExecution(new StepInstance( + new Long(11)), new JobExecution(new JobInstance(null), + new Long(12))); + try { + executor.processChunk(configuration, stepExecution); + fail("Expected RuntimeException"); + } catch (NullPointerException e) { + // expected + } + assertEquals(1, list.size()); + } + public void testUnsuccessfulWrongConfiguration() throws Exception { try { factory.getExecutor(new StepConfigurationSupport()); @@ -87,13 +120,12 @@ public class SimpleStepExecutorFactoryTests extends TestCase { fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected - assertTrue( - "Error message does not contain JobRepository: " - + e.getMessage(), e.getMessage().indexOf( - "JobRepository") >= 0); + assertTrue("Error message does not contain JobRepository: " + + e.getMessage(), + e.getMessage().indexOf("JobRepository") >= 0); } } - + public void testMandatoryProperties() throws Exception { factory = new SimpleStepExecutorFactory(); try { @@ -103,4 +135,22 @@ public class SimpleStepExecutorFactoryTests extends TestCase { // expected } } + + /** + * @author Dave Syer + * + */ + public class SimpleHolderStepConfiguration extends SimpleStepConfiguration + implements RepeatOperationsHolder { + private RepeatOperations executor; + + public SimpleHolderStepConfiguration(RepeatOperations executor) { + this.executor = executor; + } + + public RepeatOperations getChunkOperations() { + return executor; + } + } + } diff --git a/infrastructure/src/main/java/org/springframework/batch/repeat/support/RepeatTemplate.java b/infrastructure/src/main/java/org/springframework/batch/repeat/support/RepeatTemplate.java index d7b4c7cce..4a332e773 100644 --- a/infrastructure/src/main/java/org/springframework/batch/repeat/support/RepeatTemplate.java +++ b/infrastructure/src/main/java/org/springframework/batch/repeat/support/RepeatTemplate.java @@ -235,7 +235,8 @@ public class RepeatTemplate implements RepeatOperations { for (int i = interceptors.length; i-- > 0;) { RepeatInterceptor interceptor = interceptors[i]; interceptor.onError(context, t); - logger.error("Exception intercepted (" + (i + 1) + " of " + interceptors.length + ")", t); + // This is not an error - only log at debug level. + logger.debug("Exception intercepted (" + (i + 1) + " of " + interceptors.length + ")", t); } }