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

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

Introduction

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

Prototype

public void setSkippableExceptionClasses(Map<Class<? extends Throwable>, Boolean> exceptionClasses) 

Source Link

Document

Exception classes that when raised won't crash the job but will result in the item which handling caused the exception being skipped.

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//from  w ww .  j  a v  a2 s .  co  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());
}