Change logging level of error in RepeatTemplate
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user