|
| 1 | +package org.baeldung.spring_batch_intro; |
| 2 | + |
| 3 | +import java.net.MalformedURLException; |
| 4 | +import java.text.ParseException; |
| 5 | + |
| 6 | +import org.baeldung.spring_batch_intro.model.Transaction; |
| 7 | +import org.baeldung.spring_batch_intro.service.CustomItemProcessor; |
| 8 | +import org.baeldung.spring_batch_intro.service.RecordFieldSetMapper; |
| 9 | +import org.springframework.batch.core.Job; |
| 10 | +import org.springframework.batch.core.Step; |
| 11 | +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; |
| 12 | +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; |
| 13 | +import org.springframework.batch.item.ItemProcessor; |
| 14 | +import org.springframework.batch.item.ItemReader; |
| 15 | +import org.springframework.batch.item.ItemWriter; |
| 16 | +import org.springframework.batch.item.UnexpectedInputException; |
| 17 | +import org.springframework.batch.item.file.FlatFileItemReader; |
| 18 | +import org.springframework.batch.item.file.mapping.DefaultLineMapper; |
| 19 | +import org.springframework.batch.item.file.transform.DelimitedLineTokenizer; |
| 20 | +import org.springframework.batch.item.xml.StaxEventItemWriter; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 23 | +import org.springframework.beans.factory.annotation.Value; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.core.io.Resource; |
| 26 | +import org.springframework.oxm.Marshaller; |
| 27 | +import org.springframework.oxm.jaxb.Jaxb2Marshaller; |
| 28 | + |
| 29 | +public class SpringBatchConfig { |
| 30 | + @Autowired |
| 31 | + private JobBuilderFactory jobs; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private StepBuilderFactory steps; |
| 35 | + |
| 36 | + @Value("input/record.csv") |
| 37 | + private Resource inputCsv; |
| 38 | + |
| 39 | + @Value("file:xml/output.xml") |
| 40 | + private Resource outputXml; |
| 41 | + |
| 42 | + @Bean |
| 43 | + public ItemReader<Transaction> itemReader() |
| 44 | + throws UnexpectedInputException, ParseException { |
| 45 | + FlatFileItemReader<Transaction> reader = new FlatFileItemReader<Transaction>(); |
| 46 | + DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer(); |
| 47 | + String[] tokens = { "username", "userid", "transactiondate", "amount" }; |
| 48 | + tokenizer.setNames(tokens); |
| 49 | + reader.setResource(inputCsv); |
| 50 | + DefaultLineMapper<Transaction> lineMapper = new DefaultLineMapper<Transaction>(); |
| 51 | + lineMapper.setLineTokenizer(tokenizer); |
| 52 | + lineMapper.setFieldSetMapper(new RecordFieldSetMapper()); |
| 53 | + reader.setLinesToSkip(1); |
| 54 | + reader.setLineMapper(lineMapper); |
| 55 | + return reader; |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + public ItemProcessor<Transaction, Transaction> itemProcessor() { |
| 60 | + return new CustomItemProcessor(); |
| 61 | + } |
| 62 | + |
| 63 | + @Bean |
| 64 | + public ItemWriter<Transaction> itemWriter(Marshaller marshaller) |
| 65 | + throws MalformedURLException { |
| 66 | + StaxEventItemWriter<Transaction> itemWriter = new StaxEventItemWriter<Transaction>(); |
| 67 | + itemWriter.setMarshaller(marshaller); |
| 68 | + itemWriter.setRootTagName("transactionRecord"); |
| 69 | + itemWriter.setResource(outputXml); |
| 70 | + return itemWriter; |
| 71 | + } |
| 72 | + |
| 73 | + @Bean |
| 74 | + public Marshaller marshaller() { |
| 75 | + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); |
| 76 | + marshaller.setClassesToBeBound(new Class[] { Transaction.class }); |
| 77 | + return marshaller; |
| 78 | + } |
| 79 | + |
| 80 | + @Bean |
| 81 | + protected Step step1(ItemReader<Transaction> reader, |
| 82 | + ItemProcessor<Transaction, Transaction> processor, |
| 83 | + ItemWriter<Transaction> writer) { |
| 84 | + return steps.get("step1").<Transaction, Transaction> chunk(10) |
| 85 | + .reader(reader).processor(processor).writer(writer).build(); |
| 86 | + } |
| 87 | + |
| 88 | + @Bean(name = "firstBatchJob") |
| 89 | + public Job job(@Qualifier("step1") Step step1) { |
| 90 | + return jobs.get("firstBatchJob").start(step1).build(); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments