From f45da1aa74ef7f4b981a7a95039fdda3a5d7d5a9 Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 5 Aug 2008 21:54:43 +0000 Subject: [PATCH] OPEN - issue BATCH-753: Listener exception handling Add SkipListenerFailedException to write skip listener call --- .../step/item/SkipLimitStepFactoryBean.java | 44 ++++++++++++------- .../item/SkipListenerFailedException.java | 6 +-- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SkipLimitStepFactoryBean.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SkipLimitStepFactoryBean.java index 268483980..0a929286a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SkipLimitStepFactoryBean.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SkipLimitStepFactoryBean.java @@ -244,23 +244,28 @@ public class SkipLimitStepFactoryBean extends SimpleStepFactoryBean { List> exceptions = new ArrayList>(Arrays.asList(skippableExceptionClasses)); ItemSkipPolicy readSkipPolicy = new LimitCheckingItemSkipPolicy(skipLimit, exceptions, - new ArrayList>(){{ - for (Class exceptionClass : fatalExceptionClasses) { - add(exceptionClass); + new ArrayList>() { + { + for (Class exceptionClass : fatalExceptionClasses) { + add(exceptionClass); + } } - }}); - exceptions.addAll( - new ArrayList>(){{ - for (Class exceptionClass : retryableExceptionClasses) { - add(exceptionClass); - } - }}); + }); + exceptions.addAll(new ArrayList>() { + { + for (Class exceptionClass : retryableExceptionClasses) { + add(exceptionClass); + } + } + }); ItemSkipPolicy writeSkipPolicy = new LimitCheckingItemSkipPolicy(skipLimit, exceptions, - new ArrayList>(){{ - for (Class exceptionClass : fatalExceptionClasses) { - add(exceptionClass); + new ArrayList>() { + { + for (Class exceptionClass : fatalExceptionClasses) { + add(exceptionClass); + } } - }}); + }); StatefulRetryItemHandler itemHandler = new StatefulRetryItemHandler(getItemReader(), getItemWriter(), retryTemplate, itemKeyGenerator, readSkipPolicy, writeSkipPolicy); itemHandler.setSkipListeners(BatchListenerFactoryHelper.getSkipListeners(getListeners())); @@ -335,8 +340,8 @@ public class SkipLimitStepFactoryBean extends SimpleStepFactoryBean { * @param listeners */ public void setSkipListeners(SkipListener[] listeners) { - for (SkipListener listener1 : listeners) { - registerSkipListener(listener1); + for (SkipListener listener : listeners) { + registerSkipListener(listener); } } @@ -416,7 +421,12 @@ public class SkipLimitStepFactoryBean extends SimpleStepFactoryBean { public Object recover(RetryContext context) { Throwable t = context.getLastThrowable(); if (writeSkipPolicy.shouldSkip(t, contribution.getStepSkipCount())) { - listener.onSkipInWrite(item, t); + try { + listener.onSkipInWrite(item, t); + } + catch (RuntimeException ex) { + throw new SkipListenerFailedException("Fatal exception in SkipListener.", ex, t); + } } contribution.incrementWriteSkipCount(); return null; diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SkipListenerFailedException.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SkipListenerFailedException.java index 0a9b7fe12..02cb95948 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SkipListenerFailedException.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/SkipListenerFailedException.java @@ -31,10 +31,10 @@ public class SkipListenerFailedException extends UnexpectedJobExecutionException /** * @param message describes the error to the user * @param ex the exception that was thrown by a {@link SkipListener} - * @param e the exception that caused the skip + * @param t the exception that caused the skip */ - public SkipListenerFailedException(String message, RuntimeException ex, Exception e) { - super(message + "\n" + e.getClass().getName() + ": " + e.getMessage(), ex); + public SkipListenerFailedException(String message, RuntimeException ex, Throwable t) { + super(message + "\n" + t.getClass().getName() + ": " + t.getMessage(), ex); } }