Example usage for org.springframework.batch.item.support ListItemReader ListItemReader

List of usage examples for org.springframework.batch.item.support ListItemReader ListItemReader

Introduction

In this page you can find the example usage for org.springframework.batch.item.support ListItemReader ListItemReader.

Prototype

public ListItemReader(List<T> list) 

Source Link

Usage

From source file:de.langmi.spring.batch.examples.readers.list.ListItemReaderTest.java

/**
 * Test should read succesfully.//ww  w .j av a 2  s. c o m
 *
 * @throws Exception 
 */
@Test
public void testSuccessfulReading() throws Exception {
    // setup reader with test data
    List<String> testData = new TestDataFactoryBean().getObject();
    reader = new ListItemReader<String>(testData);
    // read
    int count = 0;
    String line;
    while ((line = reader.read()) != null) {
        assertEquals(String.valueOf(count), line);
        count++;
    }
    assertEquals(testData.size(), count);
}

From source file:de.codecentric.batch.TestBatchConfiguration.java

@Bean
public ItemReader<String> reader() {
    return new ListItemReader<String>(Arrays.asList("1", "2", "3"));
}

From source file:configuration.JobConfiguration.java

@Bean
public Step step2() {
    return stepBuilderFactory.get("step2").<String, String>chunk(3)
            .reader(new ListItemReader<>(Arrays.asList("1", "2", "3", "4", "5", "6")))
            .processor(new ItemProcessor<String, String>() {
                @Override//from w ww  .  j  ava 2  s . c o m
                public String process(String item) throws Exception {
                    return String.valueOf(Integer.parseInt((String) item) * -1);
                }
            }).writer(new ItemWriter<String>() {
                @Override
                public void write(List<? extends String> items) throws Exception {
                    for (Object item : items) {
                        System.out.println(">> " + item);
                    }
                }
            }).build();
}

From source file:com.mycompany.spring.batch.mail.demo.batch.SampleBatchApplication.java

@Bean
public ListItemReader<String> reader() {
    List<String> items = new ArrayList<>();
    items.add("111");
    items.add("222");
    items.add("333");
    ListItemReader<String> reader = new ListItemReader<>(items);
    return reader;
}

From source file:com.github.jrrdev.mantisbtsync.core.jobs.issues.IssuesReadersConfiguration.java

/**
 * Return a reader that gets a list of issues ids from a the job parameter mantis.issues_id.
 * The list of issues ids passed in parameter must be separated by a semi-colon.
 *
 * @param authManager/*w ww  . jav  a  2 s .co  m*/
 *          The portal auth manager
 * @param clientStub
 *          Axis client stub
 * @param userName
 *          MantisBT username. If anonymous access is used, should be an empty string.
 * @param password
 *          MantisBT password. If anonymous access is used, should be an empty string.
 * @param issuesIds
 *          Semi-colon separated list of issues ids
 * @return the reader
 */
@Bean
@StepScope
public ListItemReader<BugIdBean> listIssuesReader(final PortalAuthManager authManager,
        final MantisConnectBindingStub clientStub,
        @Value("#{jobParameters['mantis.username']}") final String userName,
        @Value("#{jobParameters['mantis.password']}") final String password,
        @Value("#{jobParameters['mantis.issues_id']}") final String issuesIds) {

    final List<BugIdBean> itemList = new ArrayList<BugIdBean>();
    if (issuesIds != null && !issuesIds.isEmpty()) {
        final String[] strIds = issuesIds.split(";");
        for (final String strId : strIds) {
            final long idValue = Long.valueOf(strId);
            final BugIdBean bean = new BugIdBean();
            bean.setId(BigInteger.valueOf(idValue));
            itemList.add(bean);
        }
    }

    final ListItemReader<BugIdBean> reader = new ListItemReader<BugIdBean>(itemList);
    return reader;
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

@SuppressWarnings("unchecked")
@Before//ww w.  j av a 2  s  . c o m
public void setUp() throws Exception {

    factory = new FaultTolerantStepFactoryBean<String, String>();
    factory.setBeanName("step");

    factory.setItemReader(new ListItemReader<String>(new ArrayList<String>()));
    factory.setItemWriter(writer);
    factory.setJobRepository(repository);
    factory.setTransactionManager(new ResourcelessTransactionManager());
    factory.setRetryableExceptionClasses(getExceptionMap(Exception.class));
    factory.setCommitInterval(1); // trivial by default

    factory.setSkippableExceptionClasses(getExceptionMap(Exception.class));

    JobParameters jobParameters = new JobParametersBuilder().addString("statefulTest", "make_this_unique")
            .toJobParameters();
    jobExecution = repository.createJobExecution("job", jobParameters);
    jobExecution.setEndTime(new Date());

}

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  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.FaultTolerantStepFactoryBeanRetryTests.java

@Test
public void testProcessAllItemsWhenErrorInWriter() throws Exception {
    final int RETRY_LIMIT = 3;
    final List<String> ITEM_LIST = Arrays.asList("a", "b", "c");
    ItemWriter<String> failingWriter = new ItemWriter<String>() {
        @Override/*from   w  ww.j  ava2 s .c  o  m*/
        public void write(List<? extends String> data) throws Exception {
            int count = 0;
            for (String item : data) {
                if (count++ == 2) {
                    throw new Exception("Planned failure in writer");
                }
                written.add(item);
            }
        }
    };

    ItemProcessor<String, String> processor = new ItemProcessor<String, String>() {
        @Override
        public String process(String item) throws Exception {
            processed.add(item);
            return item;
        }
    };
    ItemReader<String> reader = new ListItemReader<String>(ITEM_LIST);
    factory.setCommitInterval(3);
    factory.setRetryLimit(RETRY_LIMIT);
    factory.setSkipLimit(1);
    @SuppressWarnings("unchecked")
    Map<Class<? extends Throwable>, Boolean> exceptionMap = getExceptionMap(Exception.class);
    factory.setSkippableExceptionClasses(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);
    assertEquals(ExitStatus.COMPLETED.getExitCode(), stepExecution.getExitStatus().getExitCode());
    /*
     * 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.FaultTolerantStepFactoryBeanRetryTests.java

@Test
public void testNoItemsReprocessedWhenErrorInWriterAndProcessorNotTransactional() throws Exception {
    ItemWriter<String> failingWriter = new ItemWriter<String>() {
        @Override/*from ww w.java2s  .  c  o  m*/
        public void write(List<? extends String> data) throws Exception {
            int count = 0;
            for (String item : data) {
                if (count++ == 2) {
                    throw new Exception("Planned failure in writer");
                }
                written.add(item);
            }
        }
    };

    ItemProcessor<String, String> processor = new ItemProcessor<String, String>() {
        @Override
        public String process(String item) throws Exception {
            processed.add(item);
            return item;
        }
    };
    ItemReader<String> reader = new ListItemReader<String>(Arrays.asList("a", "b", "c"));
    factory.setProcessorTransactional(false);
    factory.setCommitInterval(3);
    factory.setRetryLimit(3);
    factory.setSkippableExceptionClasses(new HashMap<Class<? extends Throwable>, Boolean>());
    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);
    assertEquals(3, processed.size()); // Initial try only, then cached
}

From source file:org.springframework.batch.core.step.item.FaultTolerantStepFactoryBeanRetryTests.java

/**
 * N.B. this doesn't really test retry, since the retry is only on write
 * failures, but it does test that read errors are re-presented for another
 * try when the retryLimit is high enough (it is used to build an exception
 * handler).//from  www. j  a va 2 s . c om
 *
 * @throws Exception
 */
@SuppressWarnings("unchecked")
@Test
public void testSuccessfulRetryWithReadFailure() throws Exception {
    ItemReader<String> provider = new ListItemReader<String>(Arrays.asList("a", "b", "c")) {
        @Override
        public String read() {
            String item = super.read();
            provided.add(item);
            count++;
            if (count == 2) {
                throw new RuntimeException("Temporary error - retry for success.");
            }
            return item;
        }
    };
    factory.setItemReader(provider);
    factory.setRetryLimit(10);
    factory.setSkippableExceptionClasses(getExceptionMap());
    Step step = factory.getObject();

    StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
    repository.add(stepExecution);
    step.execute(stepExecution);

    assertEquals(0, stepExecution.getSkipCount());

    // [a, b with error]
    assertEquals(2, provided.size());
    // [a]
    assertEquals(1, processed.size());
    // []
    assertEquals(0, recovered.size());
    assertEquals(1, stepExecution.getReadCount());
    assertEquals(0, stepExecution.getReadSkipCount());
}