List of usage examples for org.springframework.batch.support.transaction TransactionAwareProxyFactory createTransactionalList
public static <T> List<T> createTransactionalList(List<T> list)
From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java
@Test public void testProcessAllItemsWhenErrorInWriterTransformationWhenReaderTransactional() throws Exception { final int RETRY_LIMIT = 3; final List<String> ITEM_LIST = TransactionAwareProxyFactory .createTransactionalList(Arrays.asList("1", "2", "3")); FaultTolerantStepFactoryBean<String, Integer> factory = new FaultTolerantStepFactoryBean<String, Integer>(); factory.setBeanName("step"); factory.setJobRepository(repository); factory.setTransactionManager(new ResourcelessTransactionManager()); ItemWriter<Integer> failingWriter = new ItemWriter<Integer>() { @Override/*from w w w. j a va 2 s. c om*/ public void write(List<? extends Integer> data) throws Exception { int count = 0; for (Integer item : data) { if (count++ == 2) { throw new Exception("Planned failure in writer"); } written.add(item); } } }; ItemProcessor<String, Integer> processor = new ItemProcessor<String, Integer>() { @Override public Integer process(String item) throws Exception { processed.add(item); return Integer.parseInt(item); } }; ItemReader<String> reader = new ListItemReader<String>( TransactionAwareProxyFactory.createTransactionalList(ITEM_LIST)); factory.setCommitInterval(3); factory.setRetryLimit(RETRY_LIMIT); factory.setSkipLimit(1); factory.setIsReaderTransactionalQueue(true); @SuppressWarnings("unchecked") Map<Class<? extends Throwable>, Boolean> exceptionMap = getExceptionMap(Exception.class); factory.setSkippableExceptionClasses(exceptionMap); factory.setRetryableExceptionClasses(exceptionMap); factory.setItemReader(reader); factory.setItemProcessor(processor); factory.setItemWriter(failingWriter); Step step = factory.getObject(); StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); repository.add(stepExecution); step.execute(stepExecution); /* * Each chunk tried up to RETRY_LIMIT, then the scan processes each item * once, identifying the skip as it goes */ assertEquals((RETRY_LIMIT + 1) * ITEM_LIST.size(), processed.size()); }
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);//from 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()); }