Search in sources :

Example 1 with BeanWrapperFieldSetMapper

use of org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper in project microservices by pwillhan.

the class ContactBatchJobConfiguration method reader.

@Bean
public ItemReader<Contact> reader() {
    FlatFileItemReader<Contact> reader = new FlatFileItemReader<>();
    reader.setResource(new ClassPathResource("data.csv"));
    reader.setLineMapper(new DefaultLineMapper<Contact>() {

        {
            setLineTokenizer(new DelimitedLineTokenizer() {

                {
                    setNames(new String[] { "firstName", "lastName", "email" });
                }
            });
            setFieldSetMapper(new BeanWrapperFieldSetMapper<Contact>() {

                {
                    setTargetType(Contact.class);
                }
            });
        }
    });
    return reader;
}
Also used : FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) ClassPathResource(org.springframework.core.io.ClassPathResource) BeanWrapperFieldSetMapper(org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper) Bean(org.springframework.context.annotation.Bean)

Example 2 with BeanWrapperFieldSetMapper

use of org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper in project microservices by pwillhan.

the class BatchConfiguration method flatFileItemReader.

@Bean
@StepScope
FlatFileItemReader<Person> flatFileItemReader(@Value("#{jobParameters[file]}") File file) {
    FlatFileItemReader<Person> r = new FlatFileItemReader<>();
    r.setResource(new FileSystemResource(file));
    r.setLineMapper(new DefaultLineMapper<Person>() {

        {
            this.setLineTokenizer(new DelimitedLineTokenizer(",") {

                {
                    this.setNames(new String[] { "first", "last", "email" });
                }
            });
            this.setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {

                {
                    this.setTargetType(Person.class);
                }
            });
        }
    });
    return r;
}
Also used : FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DelimitedLineTokenizer(org.springframework.batch.item.file.transform.DelimitedLineTokenizer) FileSystemResource(org.springframework.core.io.FileSystemResource) BeanWrapperFieldSetMapper(org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper) StepScope(org.springframework.batch.core.configuration.annotation.StepScope) Bean(org.springframework.context.annotation.Bean)

Aggregations

FlatFileItemReader (org.springframework.batch.item.file.FlatFileItemReader)2 BeanWrapperFieldSetMapper (org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper)2 DelimitedLineTokenizer (org.springframework.batch.item.file.transform.DelimitedLineTokenizer)2 Bean (org.springframework.context.annotation.Bean)2 StepScope (org.springframework.batch.core.configuration.annotation.StepScope)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1