diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/callback/ItemWriterRetryCallback.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/callback/ItemWriterRetryCallback.java index a4fb00e51..c63f7db8c 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/callback/ItemWriterRetryCallback.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/callback/ItemWriterRetryCallback.java @@ -40,8 +40,6 @@ import org.springframework.batch.retry.policy.ItemWriterRetryPolicy; */ public class ItemWriterRetryCallback implements RetryCallback { - public static final String ITEM = ItemWriterRetryCallback.class.getName() + ".ITEM"; - private Object item; private ItemWriter writer; @@ -107,26 +105,20 @@ public class ItemWriterRetryCallback implements RetryCallback { public Object doWithRetry(RetryContext context) throws Throwable { // This requires a collaboration with the RetryPolicy... if (!context.isExhaustedOnly()) { - return process(context); + process(context); + return null; } throw new RetryException("Recovery path requested in retry callback."); } - public Object next(RetryContext context) { - Object item = context.getAttribute(ITEM); - if (item == null) { - item = this.item; - context.setAttribute(ITEM, item); - } + public Object getItem() { return item; } private Object process(RetryContext context) throws Exception { - Object item = next(context); if (item != null) { writer.write(item); } - context.removeAttribute(ITEM); // if successful return item; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ItemWriterRetryPolicy.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ItemWriterRetryPolicy.java index 03c564718..a8ac02046 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ItemWriterRetryPolicy.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ItemWriterRetryPolicy.java @@ -134,20 +134,20 @@ public class ItemWriterRetryPolicy extends AbstractStatefulRetryPolicy { private class ItemWriterRetryContext extends RetryContextSupport implements RetryPolicy { - private Object item; + final private Object item; // The delegate context... private RetryContext delegateContext; - private ItemRecoverer recoverer; + final private ItemRecoverer recoverer; - private ItemKeyGenerator keyGenerator; + final private ItemKeyGenerator keyGenerator; - private FailedItemIdentifier failedItemIdentifier; + final private FailedItemIdentifier failedItemIdentifier; public ItemWriterRetryContext(ItemWriterRetryCallback callback, RetryContext parent) { super(parent); - item = callback.next(this); + this.item = callback.getItem(); this.recoverer = callback.getRecoverer(); this.keyGenerator = callback.getKeyGenerator(); this.failedItemIdentifier = callback.getFailedItemIdentifier(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/callback/ItemWriterRetryCallbackTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/callback/ItemWriterRetryCallbackTests.java index c42abe27f..9d4c2bb30 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/callback/ItemWriterRetryCallbackTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/callback/ItemWriterRetryCallbackTests.java @@ -81,7 +81,7 @@ public class ItemWriterRetryCallbackTests extends TestCase { template.setRetryPolicy(new NeverRetryPolicy() { public boolean canRetry(RetryContext context) { // ...register the failed item - calls.add("item(" + count + ")=" + context.getAttribute(ItemWriterRetryCallback.ITEM)); + calls.add("item(" + count + ")=" + callback.getItem()); // Do not call the base class method - the attempt counts as // successful now if (count < 2) // only retry once @@ -112,7 +112,7 @@ public class ItemWriterRetryCallbackTests extends TestCase { template.setRetryPolicy(new NeverRetryPolicy() { public void registerThrowable(RetryContext context, Throwable throwable) throws TerminatedRetryException { // ...register the failed item - calls.add("item=" + context.getAttribute(ItemWriterRetryCallback.ITEM)); + calls.add("item=" + callback.getItem()); // Call the base class method so that the next attempt is a // failure. super.registerThrowable(context, throwable); @@ -125,7 +125,7 @@ public class ItemWriterRetryCallbackTests extends TestCase { catch (IllegalStateException e) { // expected } - // One call from the callback itslef and one from the retry policy + // One call from the callback itself and one from the retry policy assertEquals(1, count); assertEquals(1, calls.size()); assertEquals("item=bar", calls.get(0));