Example usage for org.springframework.batch.core.step.factory FaultTolerantStepFactoryBean setRetryLimit

List of usage examples for org.springframework.batch.core.step.factory FaultTolerantStepFactoryBean setRetryLimit

Introduction

In this page you can find the example usage for org.springframework.batch.core.step.factory FaultTolerantStepFactoryBean setRetryLimit.

Prototype

public void setRetryLimit(int retryLimit) 

Source Link

Document

Public setter for the retry limit.

Usage

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/* ww  w  . j  a  va 2  s.  c  o  m*/
        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());
}