List of usage examples for org.springframework.batch.core.listener SkipListenerSupport SkipListenerSupport
SkipListenerSupport
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@SuppressWarnings("unchecked") @Test//from w ww . j a v a2s . c om public void testSkipAndRetryWithWriteFailure() throws Exception { factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() { @Override public void onSkipInWrite(String item, Throwable t) { recovered.add(item); assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); } } }); factory.setSkipLimit(2); ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")) { @Override public String read() { String item = super.read(); logger.debug("Read Called! Item: [" + item + "]"); provided.add(item); count++; return item; } }; ItemWriter<String> itemWriter = new ItemWriter<String>() { @Override public void write(List<? extends String> item) throws Exception { logger.debug("Write Called! Item: [" + item + "]"); processed.addAll(item); written.addAll(item); if (item.contains("b") || item.contains("d")) { throw new RuntimeException("Write error - planned but recoverable."); } } }; factory.setItemReader(provider); factory.setItemWriter(itemWriter); factory.setRetryLimit(5); factory.setRetryableExceptionClasses(getExceptionMap(RuntimeException.class)); AbstractStep step = (AbstractStep) factory.getObject(); step.setName("mytest"); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(2, recovered.size()); assertEquals(2, stepExecution.getSkipCount()); assertEquals(2, stepExecution.getWriteSkipCount()); List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("a,c,e,f")); assertEquals(expectedOutput, written); assertEquals("[a, b, c, d, e, f, null]", provided.toString()); assertEquals("[a, b, b, b, b, b, b, c, d, d, d, d, d, d, e, f]", processed.toString()); assertEquals("[b, d]", recovered.toString()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@SuppressWarnings("unchecked") @Test/* w ww. ja va 2 s . com*/ public void testSkipAndRetryWithWriteFailureAndNonTrivialCommitInterval() throws Exception { factory.setCommitInterval(3); factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() { @Override public void onSkipInWrite(String item, Throwable t) { recovered.add(item); assertTrue(TransactionSynchronizationManager.isActualTransactionActive()); } } }); factory.setSkipLimit(2); ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")) { @Override public String read() { String item = super.read(); logger.debug("Read Called! Item: [" + item + "]"); provided.add(item); count++; return item; } }; ItemWriter<String> itemWriter = new ItemWriter<String>() { @Override public void write(List<? extends String> item) throws Exception { logger.debug("Write Called! Item: [" + item + "]"); processed.addAll(item); written.addAll(item); if (item.contains("b") || item.contains("d")) { throw new RuntimeException("Write error - planned but recoverable."); } } }; factory.setItemReader(provider); factory.setItemWriter(itemWriter); factory.setRetryLimit(5); factory.setRetryableExceptionClasses(getExceptionMap(RuntimeException.class)); AbstractStep step = (AbstractStep) factory.getObject(); step.setName("mytest"); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(2, recovered.size()); assertEquals(2, stepExecution.getSkipCount()); assertEquals(2, stepExecution.getWriteSkipCount()); List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("a,c,e,f")); assertEquals(expectedOutput, written); // [a, b, c, d, e, f, null] assertEquals(7, provided.size()); // [a, b, c, a, b, c, a, b, c, a, b, c, a, b, c, a, b, c, d, e, f, d, // e, f, d, e, f, d, e, f, d, e, f, d, e, f] // System.err.println(processed); assertEquals(36, processed.size()); // [b, d] assertEquals(2, recovered.size()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java
/** * Check items causing errors are skipped as expected. *//* ww w . jav a 2 s . c o m*/ @Test public void testSkipOverLimitOnReadWithListener() throws Exception { reader.setFailures("1", "3", "5"); writer.setFailures(); final List<Throwable> listenerCalls = new ArrayList<Throwable>(); factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() { @Override public void onSkipInRead(Throwable t) { listenerCalls.add(t); } } }); factory.setCommitInterval(2); factory.setSkipLimit(2); Step step = factory.getObject(); step.execute(stepExecution); // 1,3 skipped inside a committed chunk. 5 tripped the skip // limit but it was skipped in a chunk that rolled back, so // it will re-appear on a restart and the listener is not called. assertEquals(2, listenerCalls.size()); assertEquals(2, stepExecution.getReadSkipCount()); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java
/** * Check items causing errors are skipped as expected. *//*from www. ja v a2 s.c o m*/ @SuppressWarnings("unchecked") @Test public void testSkipListenerFailsOnRead() throws Exception { reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6")); reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5")); writer.setFailures("4"); factory.setSkipLimit(3); factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() { @Override public void onSkipInRead(Throwable t) { throw new RuntimeException("oops"); } } }); factory.setSkippableExceptionClasses(getExceptionMap(Exception.class)); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals("oops", stepExecution.getFailureExceptions().get(0).getCause().getMessage()); // listeners are called only once chunk is about to commit, so // listener failure does not affect other statistics assertEquals(2, stepExecution.getReadSkipCount()); // but we didn't get as far as the write skip in the scan: assertEquals(0, stepExecution.getWriteSkipCount()); assertEquals(2, stepExecution.getSkipCount()); assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName())); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanTests.java
/** * Check items causing errors are skipped as expected. *//*from w w w . jav a2s . c om*/ @SuppressWarnings("unchecked") @Test public void testSkipListenerFailsOnWrite() throws Exception { reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6")); writer.setFailures("4"); factory.setSkipLimit(3); factory.setListeners(new StepListener[] { new SkipListenerSupport<String, String>() { @Override public void onSkipInWrite(String item, Throwable t) { throw new RuntimeException("oops"); } } }); factory.setSkippableExceptionClasses(getExceptionMap(Exception.class)); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals("oops", stepExecution.getFailureExceptions().get(0).getCause().getMessage()); assertEquals(1, stepExecution.getSkipCount()); assertEquals(0, stepExecution.getReadSkipCount()); assertEquals(1, stepExecution.getWriteSkipCount()); assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step.getName())); }