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:fr.acxio.tools.agia.io.IdentityResourceAwareItemReaderItemStreamTest.java

@Test
public void testReadNotExists() throws Exception {
    IdentityResourceAwareItemReaderItemStream aReader = new IdentityResourceAwareItemReaderItemStream();
    aReader.setName("testReader");
    aReader.setStrict(false);//w  w  w . j  av a  2  s  . co  m

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

    aReader.setResource(aResource);

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

From source file:com.inkubator.hrm.batch.PayTempOvertimeUploadReader.java

private void initializationCsvReader(String filePath) {

    //read a CSV file
    Resource resource = new FileSystemResource(filePath);

    //split by separated coma
    DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer(DelimitedLineTokenizer.DELIMITER_COMMA);
    lineTokenizer.setNames(new String[] { "Nik", "Overtime" });

    //mapped to an object
    BeanWrapperFieldSetMapper<PayTempOvertimeFileModel> beanWrapperFieldSetMapper = new BeanWrapperFieldSetMapper<>();
    beanWrapperFieldSetMapper.setTargetType(PayTempOvertimeFileModel.class);

    DefaultLineMapper<PayTempOvertimeFileModel> lineMapper = new DefaultLineMapper<>();
    lineMapper.setLineTokenizer(lineTokenizer);
    lineMapper.setFieldSetMapper(beanWrapperFieldSetMapper);

    //initial flatFileItemReader
    csvFileReader = new FlatFileItemReader<>();
    csvFileReader.setLineMapper(lineMapper);
    csvFileReader.setResource(resource);
    csvFileReader.setLinesToSkip(1);/* w  ww .  j  a va 2  s.  c  o  m*/
    csvFileReader.open(new ExecutionContext());
}

From source file:lcn.module.batch.web.guide.support.ColumnRangePartitioner.java

/**
 * DB??  ? ? Data? "? " ?  . ExecutionContext ?
 * <code>minValue</code>  <code>maxValue</code> ?  , ?   ???
 * ?  ?  ./*  w ww  . j av a 2  s  .c o  m*/
 */
public Map<String, ExecutionContext> partition(int gridSize) {
    // ?? ?  ? 
    int min = jdbcTemplate.queryForInt("SELECT MIN(" + column + ") from " + table);

    //  ?? ?  ? 
    int max = jdbcTemplate.queryForInt("SELECT MAX(" + column + ") from " + table);

    // ? Execution? ? Data? (?)
    int targetSize = (max - min) / gridSize + 1;

    Map<String, ExecutionContext> result = new HashMap<String, ExecutionContext>();
    int number = 0;
    int start = min;
    // targetSize ? ? Data
    int end = start + targetSize - 1;

    // ? ? ? ExecutionContext ? minVlaue  maxValue  
    while (start <= max) {

        ExecutionContext value = new ExecutionContext();
        result.put("partition" + number, value);

        if (end >= max) {
            end = max;
        }
        value.putInt("minValue", start);
        value.putInt("maxValue", end);
        start += targetSize;
        end += targetSize;
        number++;
    }

    return result;

}

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

/**
 * Test shared stream factory configuration for flat file reader.
 *///from   www. j a v a  2s.co m
@Test
@SuppressWarnings("unchecked")
public void testItemReaderWithSharedStreamFactory() throws Exception {
    ItemStreamReader<Map<String, Object>> reader = (ItemStreamReader<Map<String, Object>>) context
            .getBean("itemReader-sharedFactory");
    assertNotNull(reader);

    try {
        reader.open(new ExecutionContext());
    } finally {
        reader.close();
    }
}

From source file:example.UserInitializer.java

private static List<User> readUsers(Resource resource) throws Exception {

    Scanner scanner = new Scanner(resource.getInputStream());
    String line = scanner.nextLine();
    scanner.close();/*from ww w .j  a  v  a2  s .  c  om*/

    FlatFileItemReader<User> reader = new FlatFileItemReader<User>();
    reader.setResource(resource);

    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    tokenizer.setNames(line.split(","));
    tokenizer.setStrict(false);

    DefaultLineMapper<User> lineMapper = new DefaultLineMapper<User>();
    lineMapper.setFieldSetMapper(fields -> {

        User user = new User();

        user.setEmail(fields.readString("email"));
        user.setFirstname(capitalize(fields.readString("first")));
        user.setLastname(capitalize(fields.readString("last")));
        user.setNationality(fields.readString("nationality"));

        String city = Arrays.stream(fields.readString("city").split(" "))//
                .map(StringUtils::capitalize)//
                .collect(Collectors.joining(" "));
        String street = Arrays.stream(fields.readString("street").split(" "))//
                .map(StringUtils::capitalize)//
                .collect(Collectors.joining(" "));

        try {
            user.setAddress(new Address(city, street, fields.readString("zip")));
        } catch (IllegalArgumentException e) {
            user.setAddress(new Address(city, street, fields.readString("postcode")));
        }

        user.setPicture(new Picture(fields.readString("large"), fields.readString("medium"),
                fields.readString("thumbnail")));
        user.setUsername(fields.readString("username"));
        user.setPassword(fields.readString("password"));

        return user;
    });

    lineMapper.setLineTokenizer(tokenizer);

    reader.setLineMapper(lineMapper);
    reader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
    reader.setLinesToSkip(1);
    reader.open(new ExecutionContext());

    List<User> users = new ArrayList<>();
    User user = null;

    do {

        user = reader.read();

        if (user != null) {
            users.add(user);
        }

    } while (user != null);

    return users;
}

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

@Test
public void testReadNoResourceStrict() throws Exception {
    exception.expect(ItemStreamException.class);
    IdentityResourceAwareItemReaderItemStream aReader = new IdentityResourceAwareItemReaderItemStream();
    aReader.setName("testReader");
    aReader.setStrict(true);//from   w w w.j  a va2 s.  co m

    aReader.setResource(null);

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

From source file:com.inkubator.hrm.batch.PayTempOvertimeUploadReader.java

private void initializationExcelReader(String filePath) {

    //read a Excel file
    Resource resource = new FileSystemResource(filePath);

    try {/*from   w w  w.j  a  v a 2 s . co  m*/
        //mapped to an object
        BeanPropertyRowMapper<PayTempOvertimeFileModel> rowMapper = new BeanPropertyRowMapper<>();
        rowMapper.setTargetType(PayTempOvertimeFileModel.class);
        rowMapper.afterPropertiesSet();

        //initial poiItemReader
        excelFileReader = new PoiItemReader<>();
        excelFileReader.setResource(resource);
        excelFileReader.setLinesToSkip(1);
        excelFileReader.setRowMapper(rowMapper);
        excelFileReader.afterPropertiesSet();
        excelFileReader.open(new ExecutionContext());

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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

@Test
@SuppressWarnings("unchecked")
public void testRestartItemReader() throws Exception {
    ItemStreamReader<Map<String, Object>> reader = (ItemStreamReader<Map<String, Object>>) context
            .getBean("itemReader-restart");
    assertNotNull(reader);//from ww w  .j  ava2  s.c  o m

    try {
        ExecutionContext executionContext = new ExecutionContext();
        executionContext.put("BeanIOFlatFileItemReader.read.count", new Integer(2));

        reader.open(executionContext);

        Map<String, Object> map = reader.read();
        assertNotNull(map);
        assertEquals(new Integer(3), map.get("id"));
        assertEquals("Joe", map.get("name"));
    } finally {
        reader.close();
    }
}

From source file:fr.acxio.tools.agia.file.ExtendedMultiResourceItemReader.java

/**
 * Reads the next item, jumping to next resource if necessary.
 *//*from  w ww. j a  va 2 s. co  m*/
public T read() throws Exception, UnexpectedInputException, ParseException {

    if (noInput) {
        return null;
    }

    // If there is no resource, then this is the first item, set the current
    // resource to 0 and open the first delegate.
    if (currentResource == -1) {
        currentResource = 0;
        delegate.setResource(resources[currentResource]);
        delegate.open(new ExecutionContext());
    }

    return readNextItem();
}

From source file:info.raack.appliancelabeler.data.batch.SpringItemReaderAdapter.java

public void moveToBeginning() {
    super.moveToBeginning();
    if (itemReader instanceof AbstractCursorItemReader<?>) {
        AbstractCursorItemReader<T> cursorReader = (AbstractCursorItemReader<T>) itemReader;
        cursorReader.close();/*w  w w. j av  a2s .  c o m*/
        cursorReader.open(new ExecutionContext());
    }
}