onWriteError is called when items fail in recovery mode
This commit is contained in:
@@ -529,6 +529,7 @@ public class FaultTolerantChunkProcessor<I, O> extends SimpleChunkProcessor<I, O
|
||||
outputIterator.remove();
|
||||
}
|
||||
catch (Exception e) {
|
||||
doOnWriteError(e, items);
|
||||
if (!shouldSkip(itemWriteSkipPolicy, e, -1) && !rollbackClassifier.classify(e)) {
|
||||
inputIterator.remove();
|
||||
outputIterator.remove();
|
||||
|
||||
@@ -151,7 +151,7 @@ public class SimpleChunkProcessor<I, O> implements ChunkProcessor<I>, Initializi
|
||||
doAfterWrite(items);
|
||||
}
|
||||
catch (Exception e) {
|
||||
listener.onWriteError(e, items);
|
||||
doOnWriteError(e, items);
|
||||
throw e;
|
||||
}
|
||||
|
||||
@@ -165,6 +165,9 @@ public class SimpleChunkProcessor<I, O> implements ChunkProcessor<I>, Initializi
|
||||
protected final void doAfterWrite(List<O> items) {
|
||||
listener.afterWrite(items);
|
||||
}
|
||||
protected final void doOnWriteError(Exception e, List<O> items) {
|
||||
listener.onWriteError(e, items);
|
||||
}
|
||||
|
||||
protected void writeItems(List<O> items) throws Exception {
|
||||
if (itemWriter != null) {
|
||||
|
||||
@@ -30,6 +30,8 @@ public class FaultTolerantChunkProcessorTests {
|
||||
private List<String> list = new ArrayList<String>();
|
||||
|
||||
private List<String> after = new ArrayList<String>();
|
||||
|
||||
private List<String> writeError = new ArrayList<String>();
|
||||
|
||||
private FaultTolerantChunkProcessor<String, String> processor;
|
||||
|
||||
@@ -213,22 +215,10 @@ public class FaultTolerantChunkProcessorTests {
|
||||
}
|
||||
}));
|
||||
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
|
||||
try {
|
||||
processor.process(contribution, chunk);
|
||||
fail();
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertEquals("Planned failure!", e.getMessage());
|
||||
}
|
||||
processAndExpectPlannedRuntimeException(chunk);
|
||||
processor.process(contribution, chunk);
|
||||
assertEquals(2, chunk.getItems().size());
|
||||
try {
|
||||
processor.process(contribution, chunk);
|
||||
fail();
|
||||
}
|
||||
catch (RuntimeException e) {
|
||||
assertEquals("Planned failure!", e.getMessage());
|
||||
}
|
||||
processAndExpectPlannedRuntimeException(chunk);
|
||||
assertEquals(1, chunk.getItems().size());
|
||||
processor.process(contribution, chunk);
|
||||
assertEquals(0, chunk.getItems().size());
|
||||
@@ -260,6 +250,59 @@ public class FaultTolerantChunkProcessorTests {
|
||||
}));
|
||||
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
|
||||
|
||||
processAndExpectPlannedRuntimeException(chunk);
|
||||
processor.process(contribution, chunk);
|
||||
processor.process(contribution, chunk);
|
||||
|
||||
assertEquals("[foo, bar]", list.toString());
|
||||
assertEquals("[foo, bar]", after.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnErrorInWrite() throws Exception{
|
||||
Chunk<String> chunk = new Chunk<String>(Arrays.asList("foo", "fail"));
|
||||
processor.setListeners(Arrays.asList(new ItemListenerSupport<String, String>() {
|
||||
@Override
|
||||
public void onWriteError(Exception e, List<? extends String> item) {
|
||||
writeError.addAll(item);
|
||||
}
|
||||
}));
|
||||
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
|
||||
|
||||
processAndExpectPlannedRuntimeException(chunk);//Process foo, fail
|
||||
processor.process(contribution, chunk);;//Process foo
|
||||
processAndExpectPlannedRuntimeException(chunk);//Process fail
|
||||
|
||||
assertEquals("[foo, fail, fail]", writeError.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnErrorInWriteAllItemsFail() throws Exception{
|
||||
Chunk<String> chunk = new Chunk<String>(Arrays.asList("foo", "bar"));
|
||||
processor = new FaultTolerantChunkProcessor<String, String>(new PassThroughItemProcessor<String>(),
|
||||
new ItemWriter<String>() {
|
||||
public void write(List<? extends String> items) throws Exception {
|
||||
//Always fail in writer
|
||||
throw new RuntimeException("Planned failure!");
|
||||
}
|
||||
}, batchRetryTemplate);
|
||||
processor.setListeners(Arrays.asList(new ItemListenerSupport<String, String>() {
|
||||
@Override
|
||||
public void onWriteError(Exception e, List<? extends String> item) {
|
||||
writeError.addAll(item);
|
||||
}
|
||||
}));
|
||||
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
|
||||
|
||||
processAndExpectPlannedRuntimeException(chunk);//Process foo, bar
|
||||
processAndExpectPlannedRuntimeException(chunk);//Process foo
|
||||
processAndExpectPlannedRuntimeException(chunk);//Process bar
|
||||
|
||||
assertEquals("[foo, bar, foo, bar]", writeError.toString());
|
||||
}
|
||||
|
||||
protected void processAndExpectPlannedRuntimeException(Chunk<String> chunk)
|
||||
throws Exception {
|
||||
try {
|
||||
processor.process(contribution, chunk);
|
||||
fail();
|
||||
@@ -267,10 +310,5 @@ public class FaultTolerantChunkProcessorTests {
|
||||
catch (RuntimeException e) {
|
||||
assertEquals("Planned failure!", e.getMessage());
|
||||
}
|
||||
processor.process(contribution, chunk);
|
||||
processor.process(contribution, chunk);
|
||||
|
||||
assertEquals("[foo, bar]", list.toString());
|
||||
assertEquals("[foo, bar]", after.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user