|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.sample.file.multirecordtype; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import javax.sql.DataSource; |
| 21 | + |
| 22 | +import org.springframework.batch.core.Job; |
| 23 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 24 | +import org.springframework.batch.core.configuration.annotation.StepScope; |
| 25 | +import org.springframework.batch.core.job.builder.JobBuilder; |
| 26 | +import org.springframework.batch.core.repository.JobRepository; |
| 27 | +import org.springframework.batch.core.step.builder.StepBuilder; |
| 28 | +import org.springframework.batch.item.file.FlatFileItemReader; |
| 29 | +import org.springframework.batch.item.file.FlatFileItemWriter; |
| 30 | +import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder; |
| 31 | +import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder; |
| 32 | +import org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper; |
| 33 | +import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor; |
| 34 | +import org.springframework.batch.item.file.transform.FixedLengthTokenizer; |
| 35 | +import org.springframework.batch.item.file.transform.FormatterLineAggregator; |
| 36 | +import org.springframework.batch.item.file.transform.Range; |
| 37 | +import org.springframework.batch.sample.domain.trade.CustomerCredit; |
| 38 | +import org.springframework.batch.sample.domain.trade.Trade; |
| 39 | +import org.springframework.batch.sample.domain.trade.internal.CustomerCreditFieldSetMapper; |
| 40 | +import org.springframework.batch.sample.domain.trade.internal.TradeFieldSetMapper; |
| 41 | +import org.springframework.beans.factory.annotation.Value; |
| 42 | +import org.springframework.context.annotation.Bean; |
| 43 | +import org.springframework.context.annotation.Configuration; |
| 44 | +import org.springframework.core.io.Resource; |
| 45 | +import org.springframework.core.io.WritableResource; |
| 46 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 47 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 48 | +import org.springframework.jdbc.support.JdbcTransactionManager; |
| 49 | + |
| 50 | +/** |
| 51 | + * @author Mahmoud Ben Hassine |
| 52 | + */ |
| 53 | +@Configuration |
| 54 | +@EnableBatchProcessing |
| 55 | +public class MultiRecordTypeJobConfiguration { |
| 56 | + |
| 57 | + @Bean |
| 58 | + @StepScope |
| 59 | + public FlatFileItemReader itemReader(PatternMatchingCompositeLineMapper lineMapper, |
| 60 | + @Value("#{jobParameters[inputFile]}") Resource resource) { |
| 61 | + return new FlatFileItemReaderBuilder().name("itemReader").resource(resource).lineMapper(lineMapper).build(); |
| 62 | + } |
| 63 | + |
| 64 | + @Bean |
| 65 | + public PatternMatchingCompositeLineMapper prefixMatchingLineMapper() { |
| 66 | + PatternMatchingCompositeLineMapper mapper = new PatternMatchingCompositeLineMapper(); |
| 67 | + mapper.setTokenizers(Map.of("TRAD*", tradeLineTokenizer(), "CUST*", customerLineTokenizer())); |
| 68 | + mapper.setFieldSetMappers( |
| 69 | + Map.of("TRAD*", new TradeFieldSetMapper(), "CUST*", new CustomerCreditFieldSetMapper())); |
| 70 | + return mapper; |
| 71 | + } |
| 72 | + |
| 73 | + @Bean |
| 74 | + public FixedLengthTokenizer tradeLineTokenizer() { |
| 75 | + FixedLengthTokenizer tokenizer = new FixedLengthTokenizer(); |
| 76 | + tokenizer.setNames("isin", "quantity", "price", "customer"); |
| 77 | + tokenizer.setColumns(new Range(5, 16), new Range(17, 19), new Range(20, 25), new Range(26, 34)); |
| 78 | + return tokenizer; |
| 79 | + } |
| 80 | + |
| 81 | + @Bean |
| 82 | + public FixedLengthTokenizer customerLineTokenizer() { |
| 83 | + FixedLengthTokenizer tokenizer = new FixedLengthTokenizer(); |
| 84 | + tokenizer.setNames("id", "name", "credit"); |
| 85 | + tokenizer.setColumns(new Range(5, 9), new Range(10, 18), new Range(19, 26)); |
| 86 | + return tokenizer; |
| 87 | + } |
| 88 | + |
| 89 | + @Bean |
| 90 | + @StepScope |
| 91 | + public FlatFileItemWriter itemWriter(DelegatingTradeLineAggregator delegatingTradeLineAggregator, |
| 92 | + @Value("#{jobParameters[outputFile]}") WritableResource resource) { |
| 93 | + return new FlatFileItemWriterBuilder().name("iemWriter") |
| 94 | + .resource(resource) |
| 95 | + .lineAggregator(delegatingTradeLineAggregator) |
| 96 | + .build(); |
| 97 | + } |
| 98 | + |
| 99 | + @Bean |
| 100 | + public DelegatingTradeLineAggregator delegatingTradeLineAggregator( |
| 101 | + FormatterLineAggregator<Trade> tradeLineAggregator, |
| 102 | + FormatterLineAggregator<CustomerCredit> customerLineAggregator) { |
| 103 | + DelegatingTradeLineAggregator lineAggregator = new DelegatingTradeLineAggregator(); |
| 104 | + lineAggregator.setTradeLineAggregator(tradeLineAggregator); |
| 105 | + lineAggregator.setCustomerLineAggregator(customerLineAggregator); |
| 106 | + return lineAggregator; |
| 107 | + } |
| 108 | + |
| 109 | + @Bean |
| 110 | + public FormatterLineAggregator<Trade> tradeLineAggregator() { |
| 111 | + FormatterLineAggregator<Trade> formatterLineAggregator = new FormatterLineAggregator<>(); |
| 112 | + BeanWrapperFieldExtractor<Trade> fieldExtractor = new BeanWrapperFieldExtractor<>(); |
| 113 | + fieldExtractor.setNames(new String[] { "isin", "quantity", "price", "customer" }); |
| 114 | + formatterLineAggregator.setFieldExtractor(fieldExtractor); |
| 115 | + formatterLineAggregator.setFormat("TRAD%-12s%-3d%6s%-9s"); |
| 116 | + return formatterLineAggregator; |
| 117 | + } |
| 118 | + |
| 119 | + @Bean |
| 120 | + public FormatterLineAggregator<CustomerCredit> customerLineAggregator() { |
| 121 | + FormatterLineAggregator<CustomerCredit> formatterLineAggregator = new FormatterLineAggregator<>(); |
| 122 | + BeanWrapperFieldExtractor<CustomerCredit> fieldExtractor = new BeanWrapperFieldExtractor<>(); |
| 123 | + fieldExtractor.setNames(new String[] { "id", "name", "credit" }); |
| 124 | + formatterLineAggregator.setFieldExtractor(fieldExtractor); |
| 125 | + formatterLineAggregator.setFormat("CUST%05d%-9s%08.0f"); |
| 126 | + return formatterLineAggregator; |
| 127 | + } |
| 128 | + |
| 129 | + @Bean |
| 130 | + public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager, |
| 131 | + FlatFileItemReader itemReader, FlatFileItemWriter itemWriter) { |
| 132 | + return new JobBuilder("ioSampleJob", jobRepository) |
| 133 | + .start(new StepBuilder("step1", jobRepository).chunk(2, transactionManager) |
| 134 | + .reader(itemReader) |
| 135 | + .writer(itemWriter) |
| 136 | + .build()) |
| 137 | + .build(); |
| 138 | + } |
| 139 | + |
| 140 | + // Infrastructure beans |
| 141 | + |
| 142 | + @Bean |
| 143 | + public DataSource dataSource() { |
| 144 | + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL) |
| 145 | + .addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql") |
| 146 | + .addScript("/org/springframework/batch/core/schema-hsqldb.sql") |
| 147 | + .generateUniqueName(true) |
| 148 | + .build(); |
| 149 | + } |
| 150 | + |
| 151 | + @Bean |
| 152 | + public JdbcTransactionManager transactionManager(DataSource dataSource) { |
| 153 | + return new JdbcTransactionManager(dataSource); |
| 154 | + } |
| 155 | + |
| 156 | +} |
0 commit comments