List of usage examples for org.springframework.batch.item.support ListItemReader ListItemReader
public ListItemReader(List<T> list)
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@Test public void testRestartAfterFailedWrite() throws Exception { factory.setSkipLimit(0);//from w ww. j ava 2 s. c om factory.setCommitInterval(3); AbstractItemCountingItemStreamItemReader<String> reader = new AbstractItemCountingItemStreamItemReader<String>() { private ItemReader<String> reader; @Override protected void doClose() throws Exception { reader = null; } @Override protected void doOpen() throws Exception { reader = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")); } @Override protected String doRead() throws Exception { return reader.read(); } }; // Need to set name or else reader will fail to open reader.setName("foo"); factory.setItemReader(reader); factory.setStreams(new ItemStream[] { reader }); factory.setItemWriter(new ItemWriter<String>() { @Override public void write(List<? extends String> items) throws Exception { if (fail && items.contains("e")) { throw new RuntimeException("Planned failure"); } processed.addAll(items); } }); factory.setRetryLimit(0); Step step = factory.getObject(); fail = true; StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals(4, stepExecution.getWriteCount()); assertEquals(6, stepExecution.getReadCount()); fail = false; ExecutionContext executionContext = stepExecution.getExecutionContext(); stepExecution = new StepExecution(step.getName(), jobExecution); stepExecution.setExecutionContext(executionContext); repository.add(stepExecution); step.execute(stepExecution); assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); assertEquals(2, stepExecution.getWriteCount()); assertEquals(2, stepExecution.getReadCount()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@Test public void testSkipAndRetry() throws Exception { factory.setSkipLimit(2);/* ww w. j av a2 s. c om*/ ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c", "d", "e", "f")) { @Override public String read() { String item = super.read(); count++; if ("b".equals(item) || "d".equals(item)) { throw new RuntimeException("Read error - planned but skippable."); } return item; } }; factory.setItemReader(provider); factory.setRetryLimit(10); Step step = factory.getObject(); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(2, stepExecution.getSkipCount()); // b is processed once and skipped, plus 1, plus c, plus the null at end assertEquals(7, count); assertEquals(4, stepExecution.getReadCount()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@SuppressWarnings("unchecked") @Test//from www .ja v a2 s . 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/*from w w w. ja v a 2s . c om*/ 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.FaultTolerantStepFactoryBeanRetryTests.java
@Test public void testRetryWithNoSkip() throws Exception { factory.setRetryLimit(4);//from w w w.j av a 2 s .c om factory.setSkipLimit(0); ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("b")) { @Override public String read() { String item = super.read(); provided.add(item); count++; return item; } }; ItemWriter<String> itemWriter = new ItemWriter<String>() { @Override public void write(List<? extends String> item) throws Exception { processed.addAll(item); written.addAll(item); logger.debug("Write Called! Item: [" + item + "]"); throw new RuntimeException("Write error - planned but retryable."); } }; factory.setItemReader(provider); factory.setItemWriter(itemWriter); Step step = factory.getObject(); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("")); assertEquals(expectedOutput, written); assertEquals(0, stepExecution.getSkipCount()); // [b] assertEquals(1, provided.size()); // the failed items are tried up to the limit (but only precisely so if // the commit interval is 1) assertEquals("[b, b, b, b, b]", processed.toString()); // [] assertEquals(0, recovered.size()); assertEquals(1, stepExecution.getReadCount()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@SuppressWarnings("unchecked") @Test/* w ww .j av a 2s .co m*/ public void testNonSkippableException() throws Exception { // Very specific skippable exception factory.setSkippableExceptionClasses(getExceptionMap(UnsupportedOperationException.class)); // ...which is not retryable... factory.setRetryableExceptionClasses(getExceptionMap()); factory.setSkipLimit(1); ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("b")) { @Override public String read() { String item = super.read(); provided.add(item); count++; return item; } }; ItemWriter<String> itemWriter = new ItemWriter<String>() { @Override public void write(List<? extends String> item) throws Exception { processed.addAll(item); written.addAll(item); logger.debug("Write Called! Item: [" + item + "]"); throw new RuntimeException("Write error - planned but not skippable."); } }; factory.setItemReader(provider); factory.setItemWriter(itemWriter); Step step = factory.getObject(); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); String message = stepExecution.getFailureExceptions().get(0).getMessage(); assertTrue("Wrong message: " + message, message.contains("Write error - planned but not skippable.")); List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("")); assertEquals(expectedOutput, written); assertEquals(0, stepExecution.getSkipCount()); // [b] assertEquals("[b]", provided.toString()); // [b] assertEquals("[b]", processed.toString()); // [] assertEquals(0, recovered.size()); assertEquals(1, stepExecution.getReadCount()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@Test public void testRetryPolicy() throws Exception { factory.setRetryPolicy(new SimpleRetryPolicy(4, Collections.<Class<? extends Throwable>, Boolean>singletonMap(Exception.class, true))); factory.setSkipLimit(0);//from w ww. j av a 2s. com ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("b")) { @Override public String read() { String item = super.read(); provided.add(item); count++; return item; } }; ItemWriter<String> itemWriter = new ItemWriter<String>() { @Override public void write(List<? extends String> item) throws Exception { processed.addAll(item); written.addAll(item); logger.debug("Write Called! Item: [" + item + "]"); throw new RuntimeException("Write error - planned but retryable."); } }; factory.setItemReader(provider); factory.setItemWriter(itemWriter); AbstractStep step = (AbstractStep) factory.getObject(); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("")); assertEquals(expectedOutput, written); assertEquals(0, stepExecution.getSkipCount()); // [b] assertEquals(1, provided.size()); assertEquals("[b, b, b, b, b]", processed.toString()); // [] assertEquals(0, recovered.size()); assertEquals(1, stepExecution.getReadCount()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRollbackTests.java
@Test public void testSkipInWriterTransactionalReader() throws Exception { writer.setFailures("4"); ItemReader<String> reader = new ListItemReader<String>( TransactionAwareProxyFactory.createTransactionalList(Arrays.asList("1", "2", "3", "4", "5"))); factory.setItemReader(reader);/*w w w .j a v a 2 s .c om*/ factory.setCommitInterval(30); factory.setSkipLimit(10); factory.setIsReaderTransactionalQueue(true); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); assertEquals("[]", writer.getCommitted().toString()); assertEquals("[1, 2, 3, 4]", writer.getWritten().toString()); assertEquals("[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]", processor.getProcessed().toString()); }
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanUnexpectedRollbackTests.java
@Test @Ignore //FIXME//w w w . j a v a 2 s .c o m public void testTransactionException() throws Exception { final SkipWriterStub<String> writer = new SkipWriterStub<String>(); FaultTolerantStepFactoryBean<String, String> factory = new FaultTolerantStepFactoryBean<String, String>(); factory.setItemWriter(writer); @SuppressWarnings("serial") DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource) { private boolean failed = false; @Override protected void doCommit(DefaultTransactionStatus status) throws TransactionException { if (writer.getWritten().isEmpty() || failed || !isExistingTransaction(status.getTransaction())) { super.doCommit(status); return; } failed = true; status.setRollbackOnly(); super.doRollback(status); throw new UnexpectedRollbackException("Planned"); } }; factory.setBeanName("stepName"); factory.setTransactionManager(transactionManager); factory.setCommitInterval(2); ItemReader<String> reader = new ListItemReader<String>(Arrays.asList("1", "2")); factory.setItemReader(reader); JobRepositoryFactoryBean repositoryFactory = new JobRepositoryFactoryBean(); repositoryFactory.setDataSource(dataSource); repositoryFactory.setTransactionManager(transactionManager); repositoryFactory.afterPropertiesSet(); JobRepository repository = repositoryFactory.getObject(); factory.setJobRepository(repository); JobExecution jobExecution = repository.createJobExecution("job", new JobParameters()); StepExecution stepExecution = jobExecution.createStepExecution(factory.getName()); repository.add(stepExecution); Step step = factory.getObject(); step.execute(stepExecution); assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals("[]", writer.getCommitted().toString()); }
From source file:org.springframework.batch.core.step.tasklet.AsyncChunkOrientedStepIntegrationTests.java
private ItemReader<String> getReader(String[] args) { return new ListItemReader<String>(Arrays.asList(args)); }