OPEN - issue BATCH-486: Users must choose between skip and retry
This commit is contained in:
@@ -167,6 +167,12 @@ public class StatefulRetryStepFactoryBean extends SimpleStepFactoryBean {
|
||||
|
||||
final private ItemRecoverer itemRecoverer;
|
||||
|
||||
private AbstractItemWriter writer = new AbstractItemWriter() {
|
||||
public void write(Object item) throws Exception {
|
||||
doWrite(item);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param itemReader
|
||||
* @param itemWriter
|
||||
@@ -201,11 +207,7 @@ public class StatefulRetryStepFactoryBean extends SimpleStepFactoryBean {
|
||||
* @see org.springframework.batch.core.step.item.SimpleItemHandler#write(java.lang.Object, org.springframework.batch.core.StepContribution)
|
||||
*/
|
||||
protected void write(Object item, final StepContribution contribution) throws Exception {
|
||||
ItemWriterRetryCallback retryCallback = new ItemWriterRetryCallback(item, new AbstractItemWriter() {
|
||||
public void write(Object item) throws Exception {
|
||||
doWrite(item);
|
||||
}
|
||||
});
|
||||
ItemWriterRetryCallback retryCallback = new ItemWriterRetryCallback(item, writer);
|
||||
retryCallback.setKeyGenerator(itemKeyGenerator);
|
||||
retryCallback.setRecoverer(itemRecoverer);
|
||||
retryOperations.execute(retryCallback);
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.batch.retry.callback;
|
||||
|
||||
import org.springframework.batch.item.FailedItemIdentifier;
|
||||
import org.springframework.batch.item.ItemKeyGenerator;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemRecoverer;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.retry.RetryCallback;
|
||||
@@ -59,15 +58,16 @@ public class ItemWriterRetryCallback implements RetryCallback {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Constructor with mandatory properties.
|
||||
*
|
||||
* @param item the item to process
|
||||
* @param writer the writer to use to process it
|
||||
*/
|
||||
public ItemWriterRetryCallback(Object item, ItemWriter writer) {
|
||||
this(item, writer, null);
|
||||
}
|
||||
|
||||
public ItemWriterRetryCallback(Object item, ItemWriter writer, ItemKeyGenerator keyGenerator) {
|
||||
super();
|
||||
this.item = item;
|
||||
this.writer = writer;
|
||||
this.keyGenerator = keyGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,7 +132,7 @@ public class ItemWriterRetryCallback implements RetryCallback {
|
||||
|
||||
/**
|
||||
* Accessor for the {@link ItemRecoverer}. If the handler is null but the
|
||||
* {@link ItemReader} is an instance of {@link ItemRecoverer}, then it will
|
||||
* {@link ItemWriter} is an instance of {@link ItemRecoverer}, then it will
|
||||
* be returned instead. If none of those strategies works then a default
|
||||
* implementation of {@link ItemKeyGenerator} will be used that just returns
|
||||
* the item.
|
||||
@@ -151,7 +151,7 @@ public class ItemWriterRetryCallback implements RetryCallback {
|
||||
|
||||
/**
|
||||
* Accessor for the {@link FailedItemIdentifier}. If the handler is null
|
||||
* but the {@link ItemReader} or {@link ItemWriter} is an instance of
|
||||
* but the {@link ItemWriter} or {@link ItemWriter} is an instance of
|
||||
* {@link FailedItemIdentifier}, then it will be returned instead. If none
|
||||
* of those strategies works returns null.
|
||||
*
|
||||
@@ -169,7 +169,7 @@ public class ItemWriterRetryCallback implements RetryCallback {
|
||||
|
||||
/**
|
||||
* Accessor for the {@link ItemRecoverer}. If the handler is null but the
|
||||
* {@link ItemReader} is an instance of {@link ItemRecoverer}, then it will
|
||||
* {@link ItemWriter} is an instance of {@link ItemRecoverer}, then it will
|
||||
* be returned instead.
|
||||
*
|
||||
* @return the {@link ItemRecoverer}.
|
||||
|
||||
@@ -16,21 +16,11 @@
|
||||
|
||||
package org.springframework.batch.retry;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ItemKeyGenerator;
|
||||
import org.springframework.batch.item.ItemRecoverer;
|
||||
import org.springframework.batch.item.support.ListItemReader;
|
||||
|
||||
public class ListItemReaderRecoverer extends ListItemReader implements ItemRecoverer, ItemKeyGenerator {
|
||||
public class StubItemKeyGeneratorRecoverer implements ItemRecoverer, ItemKeyGenerator {
|
||||
|
||||
/**
|
||||
* Delegate to super class constructor.
|
||||
* @param list
|
||||
*/
|
||||
public ListItemReaderRecoverer(List list) {
|
||||
super(list);
|
||||
}
|
||||
/**
|
||||
* Do nothing. Subclassses should override to implement recovery behaviour.
|
||||
*
|
||||
@@ -17,13 +17,12 @@
|
||||
package org.springframework.batch.retry.callback;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.item.AbstractItemWriter;
|
||||
import org.springframework.batch.retry.ListItemReaderRecoverer;
|
||||
import org.springframework.batch.retry.StubItemKeyGeneratorRecoverer;
|
||||
import org.springframework.batch.retry.RetryContext;
|
||||
import org.springframework.batch.retry.RetryException;
|
||||
import org.springframework.batch.retry.TerminatedRetryException;
|
||||
@@ -39,7 +38,7 @@ public class ItemWriterRetryCallbackTests extends TestCase {
|
||||
|
||||
RetryTemplate template;
|
||||
|
||||
ListItemReaderRecoverer provider;
|
||||
StubItemKeyGeneratorRecoverer recoverer;
|
||||
|
||||
ItemWriterRetryCallback callback;
|
||||
|
||||
@@ -48,7 +47,7 @@ public class ItemWriterRetryCallbackTests extends TestCase {
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
template = new RetryTemplate();
|
||||
provider = new ListItemReaderRecoverer(Arrays.asList(new String[] { "foo" })) {
|
||||
recoverer = new StubItemKeyGeneratorRecoverer() {
|
||||
public boolean recover(Object data, Throwable cause) {
|
||||
count++;
|
||||
calls.add(data);
|
||||
@@ -144,12 +143,12 @@ public class ItemWriterRetryCallbackTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testGetKey() throws Exception {
|
||||
callback.setKeyGenerator(provider);
|
||||
callback.setKeyGenerator(recoverer);
|
||||
assertEquals("key0", callback.getKeyGenerator().getKey("foo"));
|
||||
}
|
||||
|
||||
public void testRecoverWithoutSession() throws Exception {
|
||||
provider.recover("foo", null);
|
||||
recoverer.recover("foo", null);
|
||||
assertEquals(1, count);
|
||||
assertEquals(1, calls.size());
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.batch.retry.policy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@@ -30,9 +29,9 @@ import org.springframework.batch.item.support.ListItemReader;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.context.RepeatContextSupport;
|
||||
import org.springframework.batch.repeat.support.RepeatSynchronizationManager;
|
||||
import org.springframework.batch.retry.ListItemReaderRecoverer;
|
||||
import org.springframework.batch.retry.RetryCallback;
|
||||
import org.springframework.batch.retry.RetryContext;
|
||||
import org.springframework.batch.retry.StubItemKeyGeneratorRecoverer;
|
||||
import org.springframework.batch.retry.callback.ItemWriterRetryCallback;
|
||||
import org.springframework.batch.retry.context.RetryContextSupport;
|
||||
import org.springframework.batch.retry.support.RetryTemplate;
|
||||
@@ -41,7 +40,7 @@ public class ItemWriterRetryPolicyTests extends TestCase {
|
||||
|
||||
private ItemWriterRetryPolicy policy = new ItemWriterRetryPolicy();
|
||||
|
||||
private ListItemReaderRecoverer reader;
|
||||
private StubItemKeyGeneratorRecoverer recoverer;
|
||||
|
||||
private int count = 0;
|
||||
|
||||
@@ -57,7 +56,7 @@ public class ItemWriterRetryPolicyTests extends TestCase {
|
||||
super.setUp();
|
||||
// The list simulates a failed delivery, redelivery of the same message,
|
||||
// then a new message...
|
||||
reader = new ListItemReaderRecoverer(Arrays.asList(new String[] { "foo", "foo", "bar" })) {
|
||||
recoverer = new StubItemKeyGeneratorRecoverer() {
|
||||
public boolean recover(Object data, Throwable cause) {
|
||||
count++;
|
||||
list.add(data);
|
||||
@@ -168,7 +167,7 @@ public class ItemWriterRetryPolicyTests extends TestCase {
|
||||
public void write(Object data) {
|
||||
}
|
||||
});
|
||||
callback.setRecoverer(reader);
|
||||
callback.setRecoverer(recoverer);
|
||||
RetryContext context = policy.open(callback, null);
|
||||
assertNotNull(context);
|
||||
assertTrue(policy.canRetry(context));
|
||||
@@ -210,7 +209,7 @@ public class ItemWriterRetryPolicyTests extends TestCase {
|
||||
throw new RuntimeException("Barf!");
|
||||
}
|
||||
});
|
||||
callback.setRecoverer(reader);
|
||||
callback.setRecoverer(recoverer);
|
||||
RetryTemplate template = new RetryTemplate();
|
||||
template.setRetryPolicy(policy);
|
||||
Object result = null;
|
||||
|
||||
Reference in New Issue
Block a user