Example usage for org.springframework.batch.item ExecutionContext ExecutionContext

List of usage examples for org.springframework.batch.item ExecutionContext ExecutionContext

Introduction

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

Prototype

public ExecutionContext() 

Source Link

Document

Default constructor.

Usage

From source file:org.beanio.spring.BeanIOFlatFileItemReaderTest.java

@Test
public void testInputFileNotFound() throws Exception {
    BeanIOFlatFileItemReader<Object> reader = new BeanIOFlatFileItemReader<Object>();
    reader.setStrict(false);/*from   w  w w .  j a  va2  s.c  o m*/
    reader.setStreamName("stream1");
    reader.setStreamMapping(new ClassPathResource("spring_mapping1.xml", getClass()));
    reader.setResource(new ClassPathResource("doesnotexist.txt", getClass()));
    reader.afterPropertiesSet();

    ExecutionContext ctx = new ExecutionContext();

    reader.open(ctx);
    assertNull(reader.read());
}

From source file:com.ifeng.computing.batch.job.partitioner.RangePartitioner.java

@Override
public Map<String, ExecutionContext> partition(int gridSize) {

    Map<String, ExecutionContext> result = new HashMap<String, ExecutionContext>();

    int range = 100;
    int fromId = 81289;
    int toId = range + fromId;

    for (int i = 1; i <= gridSize; i++) {
        ExecutionContext value = new ExecutionContext();

        log.debug("\nStarting : Thread" + i);
        log.debug("fromId : " + fromId);
        log.debug("toId : " + toId);

        value.putInt("fromId", fromId);
        value.putInt("toId", toId);

        // give each thread a name, thread 1,2,3
        value.putString("name", "Thread" + i);

        result.put("partition" + i, value);

        fromId = toId + 1;/*ww  w  .j  ava 2s  .com*/
        toId += range;

    }

    return result;
}

From source file:de.mediait.batch.FlatFileItemReaderTest.java

@Test
public void testFlatFileReader() throws Exception {

    final FlatFileItemReader<String[]> reader = createFlatFileReader(';', '\'');

    reader.setResource(new ClassPathResource("csv-fix-samples/fixsemicolon.txt"));

    final ExecutionContext executionContext = new ExecutionContext();
    reader.open(executionContext);// ww w .  jav  a2  s  .com
    final String[] object = (String[]) reader.read();
    reader.close();

    assertArrayEquals(new String[] { "begin", "abc' \"d\" 'ef", "end" }, object);
}

From source file:org.openmrs.module.bahmniexports.example.domain.trade.internal.FlatFileCustomerCreditDao.java

@Override
public void writeCredit(CustomerCredit customerCredit) throws Exception {

    if (!opened) {
        open(new ExecutionContext());
    }/* ww w. ja v a 2  s  . c  o m*/

    String line = "" + customerCredit.getName() + separator + customerCredit.getCredit();

    itemWriter.write(Collections.singletonList(line));
}

From source file:com.cocktail.initializer.ItemReader.java

/**
 * Read items.// w ww  .  j  a v  a 2 s  . c om
 *
 * @param <I>
 *            the generic type
 * @param path
 *            the path
 * @param itemMapper
 *            the item mapper
 * @return the list
 * @throws Exception
 *             the exception
 */
public static <I> List<I> readItems(String path, FieldSetMapper<I> itemMapper) throws Exception {

    ClassPathResource resource = new ClassPathResource(path);
    Scanner scanner = new Scanner(resource.getInputStream());
    String line = scanner.nextLine();
    scanner.close();

    FlatFileItemReader<I> itemReader = new FlatFileItemReader<I>();
    itemReader.setResource(resource);

    // DelimitedLineTokenizer defaults to | as its delimiter
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer("|");
    tokenizer.setNames(line.split("\\|"));
    tokenizer.setStrict(false);

    DefaultLineMapper<I> lineMapper = new DefaultLineMapper<I>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(itemMapper);
    itemReader.setLineMapper(lineMapper);
    itemReader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
    itemReader.setLinesToSkip(1);
    itemReader.open(new ExecutionContext());

    List<I> items = new ArrayList<>();
    I item = null;

    do {

        item = itemReader.read();

        if (item != null) {
            items.add(item);
        }

    } while (item != null);

    return items;
}

From source file:fr.acxio.tools.agia.io.IdentityResourceAwareItemReaderItemStreamTest.java

@Test
public void testRead() throws Exception {
    IdentityResourceAwareItemReaderItemStream aReader = new IdentityResourceAwareItemReaderItemStream();
    aReader.setName("testReader");

    Resource aResource = mock(Resource.class);
    when(aResource.getFilename()).thenReturn("file1");
    when(aResource.getDescription()).thenReturn("file1");
    when(aResource.exists()).thenReturn(true);

    aReader.setResource(aResource);//w  w w. j ava  2  s.c  o  m

    aReader.open(new ExecutionContext());
    assertEquals(aResource, aReader.read());
    assertNull(aReader.read());
    aReader.close();
}

From source file:ru.xxlabaza.test.batch.job.RangePartitioner.java

@Override
public Map<String, ExecutionContext> partition(int gridSize) {
    long totalItems = personRepository.count();
    System.out.println("\nTotal items: " + totalItems);

    int range = (int) totalItems / gridSize;
    if (range < chunkSize) {
        throw new IllegalArgumentException();
    }/* w  ww  . jav a2s .  c  o  m*/

    return IntStream.range(0, gridSize).boxed().map(index -> {
        ExecutionContext context = new ExecutionContext();
        context.putString("name", "partition-" + index);
        context.putInt("from", index * range);
        int nextIndex = index + 1;
        int to = nextIndex * range - 1;
        if (nextIndex == gridSize) {
            to += totalItems % gridSize;
        }
        context.putInt("to", to);
        return context;
    }).map(context -> {
        System.out.format("\nCREATED PARTITION: '%s', RANGE FROM %d, TO %d\n", context.getString("name"),
                context.getInt("from"), context.getInt("to"));
        return context;
    }).collect(toMap(context -> context.getString("name"), Function.identity()));
}

From source file:no.magott.training.ex2.CurrencyImportComponentTests.java

@Test
public void canReadNOKEURFromFile() throws Exception {
    reader.open(new ExecutionContext());
    ExchangeRate exchangeRate = reader.read();
    assertThat(exchangeRate, notNullValue());
    assertThat(exchangeRate.getDate(), notNullValue());
    assertThat(exchangeRate.getExchangeRate(), not(equalTo(0.0)));
    assertThat(exchangeRate.getFrom(), notNullValue());
    assertThat(exchangeRate.getTo(), notNullValue());

    reader.close();/*  www. ja v  a 2 s.  c  o m*/
}

From source file:org.beanio.spring.BeanIOFlatFileItemReaderTest.java

@Test(expected = ItemStreamException.class)
public void testInputFileNotFoundAndStrict() throws Exception {
    BeanIOFlatFileItemReader<Object> reader = new BeanIOFlatFileItemReader<Object>();
    reader.setStreamName("stream1");
    reader.setStreamMapping(new ClassPathResource("spring_mapping1.xml", getClass()));
    reader.setResource(new ClassPathResource("doesnotexist.txt", getClass()));
    reader.afterPropertiesSet();//  w w  w  .  jav a 2 s. co  m
    reader.open(new ExecutionContext());
}

From source file:no.magott.training.ex1.SpursImportJobIntegrationTest.java

@Test
public void spursTrashedAr5ena1() throws Exception {
    int lineNumber = 1;//Reader configured to skip first (header) row

    spursMatchReader.open(new ExecutionContext());
    //Will skip through first 141 rows in file
    while (lineNumber++ < 141) {
        spursMatchReader.read();/*from  ww w  .j  ava 2s . com*/
    }
    SpursMatch spursMatch = spursMatchReader.read();
    assertThat(spursMatch.isSpursWin(), is(true));
    assertThat(spursMatch.getOpposition(), equalTo("Arsenal"));
    assertThat(spursMatch.getSpursGoals(), equalTo(5));
    assertThat(spursMatch.getOppositionGoals(), equalTo(1));
}