Example usage for org.springframework.batch.item ItemReader read

List of usage examples for org.springframework.batch.item ItemReader read

Introduction

In this page you can find the example usage for org.springframework.batch.item ItemReader read.

Prototype

@Nullable
T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException;

Source Link

Document

Reads a piece of input data and advance to the next one.

Usage

From source file:fr.acxio.tools.agia.item.MultiLineNodeListItemReaderTest.java

@Test
public void testRead() throws Exception {
    MultiLineNodeListItemReader aReader = new MultiLineNodeListItemReader();

    ItemReader<FieldSet> aDelegate = mock(FieldSetItemReader.class);

    when(aDelegate.read()).thenReturn(
            new DefaultFieldSet(new String[] { "Type1", "123" }, new String[] { "Type", "Value" }),
            new DefaultFieldSet(new String[] { "Type2", "ABC", "2014-08-14" },
                    new String[] { "Type", "Value1", "Value2" }),
            new DefaultFieldSet(new String[] { "Type1", "345" }, new String[] { "Type", "Value" }),
            new DefaultFieldSet(new String[] { "Type2", "DEF", "2014-08-13" },
                    new String[] { "Type", "Value1", "Value2" }),
            new DefaultFieldSet(new String[] { "Type3", "789GHI", null },
                    new String[] { "Type", "Value1", "Value2" }),
            null);/*from  w  w  w.  j a  v a2s  .  com*/

    aReader.setDelegate(aDelegate);
    aReader.setNewRecordCondition("@{#next == null or #next['Type'].equals('Type1')}");

    aReader.open(new ExecutionContext());
    List<FieldSet> aRecord1 = aReader.read();
    List<FieldSet> aRecord2 = aReader.read();
    List<FieldSet> aRecord3 = aReader.read();
    aReader.close();

    assertNotNull(aRecord1);
    assertNotNull(aRecord2);
    assertNull(aRecord3);
    assertEquals(2, aRecord1.size());
    assertEquals(3, aRecord2.size());
    assertEquals("123", aRecord1.get(0).getValues()[1]);
    assertEquals("ABC", aRecord1.get(1).getValues()[1]);
    assertEquals("345", aRecord2.get(0).getValues()[1]);
    assertEquals("DEF", aRecord2.get(1).getValues()[1]);
    assertEquals("789GHI", aRecord2.get(2).getValues()[1]);
}

From source file:org.springframework.batch.item.database.JdbcPagingItemReaderAsyncTests.java

/**
 * @throws Exception//from  w  w  w. ja v  a 2  s  .c  om
 * @throws InterruptedException
 * @throws ExecutionException
 */
private void doTest() throws Exception, InterruptedException, ExecutionException {
    final ItemReader<Foo> reader = getItemReader();
    CompletionService<List<Foo>> completionService = new ExecutorCompletionService<List<Foo>>(
            Executors.newFixedThreadPool(THREAD_COUNT));
    for (int i = 0; i < THREAD_COUNT; i++) {
        completionService.submit(new Callable<List<Foo>>() {
            @Override
            public List<Foo> call() throws Exception {
                List<Foo> list = new ArrayList<Foo>();
                Foo next = null;
                do {
                    next = reader.read();
                    Thread.sleep(10L);
                    logger.debug("Reading item: " + next);
                    if (next != null) {
                        list.add(next);
                    }
                } while (next != null);
                return list;
            }
        });
    }
    int count = 0;
    Set<Foo> results = new HashSet<Foo>();
    for (int i = 0; i < THREAD_COUNT; i++) {
        List<Foo> items = completionService.take().get();
        count += items.size();
        logger.debug("Finished items count: " + items.size());
        logger.debug("Finished items: " + items);
        assertNotNull(items);
        results.addAll(items);
    }
    assertEquals(ITEM_COUNT, count);
    assertEquals(ITEM_COUNT, results.size());
}

From source file:org.springframework.batch.item.database.JdbcPagingRestartIntegrationTests.java

@Test
@Ignore //FIXME/*from  w w w .ja  v a  2s  . c om*/
public void testReaderFromStart() throws Exception {

    ItemReader<Foo> reader = getItemReader();

    int total = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS");

    ExecutionContext executionContext = new ExecutionContext();
    ((ItemStream) reader).open(executionContext);

    for (int i = 0; i < total; i++) {
        Foo item = reader.read();
        logger.debug("Item: " + item);
        assertNotNull(item);
    }

    Foo item = reader.read();
    logger.debug("Item: " + item);
    assertNull(item);

}

From source file:org.springframework.batch.item.database.JdbcPagingRestartIntegrationTests.java

@Test
@Ignore //FIXME//from w  ww. j  av a 2s.c  om
public void testReaderOnRestart() throws Exception {

    ItemReader<Foo> reader = getItemReader();

    int total = JdbcTestUtils.countRowsInTable(jdbcTemplate, "T_FOOS");
    int count = (total / pageSize) * pageSize;
    int pagesToRead = Math.min(3, total / pageSize);
    if (count >= pagesToRead * pageSize) {
        count -= pagesToRead * pageSize;
    }

    ExecutionContext executionContext = new ExecutionContext();
    executionContext.putInt("JdbcPagingItemReader.read.count", count);
    // Assume the primary keys are in order

    List<Map<String, Object>> ids = jdbcTemplate.queryForList("SELECT ID,NAME FROM T_FOOS ORDER BY ID ASC");
    logger.debug("Ids: " + ids);
    int startAfterValue = (new Long(ids.get(count - 1).get("ID").toString())).intValue();
    logger.debug("Start after: " + startAfterValue);
    Map<String, Object> startAfterValues = new LinkedHashMap<String, Object>();
    startAfterValues.put("ID", startAfterValue);
    executionContext.put("JdbcPagingItemReader.start.after", startAfterValues);
    ((ItemStream) reader).open(executionContext);

    for (int i = count; i < total; i++) {
        Foo item = reader.read();
        logger.debug("Item: " + item);
        assertNotNull(item);
    }

    Foo item = reader.read();
    logger.debug("Item: " + item);
    assertNull(item);

}