|
| 1 | +package com.fd.tryout.springbatch.config; |
| 2 | + |
| 3 | +import com.fd.tryout.springbatch.batch.PersonProcessor; |
| 4 | +import com.fd.tryout.springbatch.batch.PersonStreamWriter; |
| 5 | +import com.fd.tryout.springbatch.batch.ToUpperCaseJobExecutionListener; |
| 6 | +import com.fd.tryout.springbatch.model.Person; |
| 7 | +import org.springframework.batch.core.Job; |
| 8 | +import org.springframework.batch.core.Step; |
| 9 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 10 | +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; |
| 11 | +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; |
| 12 | +import org.springframework.batch.item.ItemStreamWriter; |
| 13 | +import org.springframework.batch.item.file.FlatFileItemReader; |
| 14 | +import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; |
| 15 | +import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.context.annotation.Bean; |
| 18 | +import org.springframework.context.annotation.Configuration; |
| 19 | +import org.springframework.core.io.ClassPathResource; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author furkand |
| 23 | + * 10/26/2018 5:57 PM |
| 24 | + */ |
| 25 | +@Configuration |
| 26 | +@EnableBatchProcessing |
| 27 | +public class Config { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + public JobBuilderFactory jobBuilderFactory; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + public StepBuilderFactory stepBuilderFactory; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private Step toUpperCaseConverter; |
| 37 | + |
| 38 | + @Bean |
| 39 | + public FlatFileItemReader<Person> reader() { |
| 40 | + return new FlatFileItemReaderBuilder<Person>() |
| 41 | + .name("personReader") |
| 42 | + .resource(new ClassPathResource("data.csv")) |
| 43 | + .delimited() |
| 44 | + .names(new String[]{"name", "surname"}) |
| 45 | + .fieldSetMapper(new BeanWrapperFieldSetMapper<Person>() {{ |
| 46 | + setTargetType(Person.class); |
| 47 | + }}) |
| 48 | + .build(); |
| 49 | + } |
| 50 | + |
| 51 | + @Bean |
| 52 | + public PersonProcessor processor() { |
| 53 | + return new PersonProcessor(); |
| 54 | + } |
| 55 | + |
| 56 | + @Bean |
| 57 | + public PersonStreamWriter writer() { |
| 58 | + return new PersonStreamWriter(); |
| 59 | + } |
| 60 | + |
| 61 | + @Bean |
| 62 | + public ToUpperCaseJobExecutionListener listener() { |
| 63 | + return new ToUpperCaseJobExecutionListener(); |
| 64 | + } |
| 65 | + |
| 66 | + @Bean("toUpperCaseJob") |
| 67 | + public Job convertToUpperCaseJob(ToUpperCaseJobExecutionListener listener) { |
| 68 | + return this.jobBuilderFactory.get("toUpperCaseJob") |
| 69 | + .listener(listener) |
| 70 | + .flow(this.toUpperCaseConverter) |
| 71 | + .end() |
| 72 | + .build(); |
| 73 | + } |
| 74 | + |
| 75 | + @Bean |
| 76 | + public Step toUpperCaseConverter(PersonStreamWriter writer) { |
| 77 | + return this.stepBuilderFactory.get("toUpperCase") |
| 78 | + .<Person, Person>chunk(10) |
| 79 | + .reader(this.reader()) |
| 80 | + .processor(this.processor()) |
| 81 | + .writer(this.writer()) |
| 82 | + .build(); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments