1
StepConfig.java

package electronicdocumentdeliverybatch.config;

import electronicdocumentdeliverybatch.models.*;
import electronicdocumentdeliverybatch.models.properties.TestFileProperties;
import electronicdocumentdeliverybatch.models.stepModals.ElectronicDocumentsBatchOutput;
import electronicdocumentdeliverybatch.utils.FlatFileConstants;
import org.springframework.amqp.core.Message;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileFooterCallback;
import org.springframework.batch.item.file.FlatFileHeaderCallback;
import org.springframework.batch.item.file.FlatFileItemWriter;
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
import org.springframework.batch.item.file.transform.FormatterLineAggregator;
import org.springframework.batch.item.support.ClassifierCompositeItemWriter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.classify.SubclassClassifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;

import java.util.HashMap;

@Configuration
public class StepConfig {

    private <T> FormatterLineAggregator<T> createLineAggregator(String[] fieldNames, String fieldFormat) {
        FormatterLineAggregator<T> lineAggregator = new FormatterLineAggregator<>();
        BeanWrapperFieldExtractor<T> fieldExtractor = new BeanWrapperFieldExtractor<>();
        fieldExtractor.setNames(fieldNames);
        lineAggregator.setFormat(fieldFormat);
        lineAggregator.setFieldExtractor(fieldExtractor);
        return lineAggregator;
    }

    @Bean(name = "NP01FlatFileItemWriter")
    public FlatFileItemWriter<NP01BatchOutput> np01FlatFileItemWriter(TestFileProperties testFileProperties, FlatFileFooterCallback footerCallback, FlatFileHeaderCallback headerCallback) {
        FlatFileItemWriter<NP01BatchOutput> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource(testFileProperties.getOutput().getResults()));
        writer.setAppendAllowed(true);
        writer.setLineAggregator(createLineAggregator(FlatFileConstants.NP01_CSV_FIELD_NAMES, FlatFileConstants.NP01_LINE_AGGREGATOR_FORMAT));
        writer.setFooterCallback(footerCallback);
        writer.setHeaderCallback(headerCallback);
        return writer;
    }

    @Bean(name = "FP01FlatFileItemWriter")
    public FlatFileItemWriter<FP01BatchOutput> fp01FlatFileItemWriter(TestFileProperties testFileProperties, FlatFileFooterCallback footerCallback, FlatFileHeaderCallback headerCallback) {
        FlatFileItemWriter<FP01BatchOutput> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource(testFileProperties.getOutput().getResults()));
        writer.setAppendAllowed(true);
        writer.setLineAggregator(createLineAggregator(FlatFileConstants.FP01_CSV_FIELD_NAMES, FlatFileConstants.FP01_LINE_AGGREGATOR_FORMAT));
        writer.setFooterCallback(footerCallback);
        writer.setHeaderCallback(headerCallback);
        return writer;
    }

    @Bean(name = "SJ01FlatFileItemWriter")
    public FlatFileItemWriter<SJ01BatchOutput> sj01FlatFileItemWriter(TestFileProperties testFileProperties, FlatFileFooterCallback footerCallback, FlatFileHeaderCallback headerCallback) {
        FlatFileItemWriter<SJ01BatchOutput> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource(testFileProperties.getOutput().getResults()));
        writer.setAppendAllowed(true);
        writer.setLineAggregator(createLineAggregator(FlatFileConstants.SJ01_CSV_FIELD_NAMES, FlatFileConstants.SJ01_LINE_AGGREGATOR_FORMAT));
        writer.setFooterCallback(footerCallback);
        writer.setHeaderCallback(headerCallback);
        return writer;
    }

    @Bean(name = "PR01FlatFileItemWriter")
    public FlatFileItemWriter<PR01BatchOutput> pr01FlatFileItemWriter(TestFileProperties testFileProperties, FlatFileFooterCallback footerCallback, FlatFileHeaderCallback headerCallback) {
        FlatFileItemWriter<PR01BatchOutput> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource(testFileProperties.getOutput().getResults()));
        writer.setAppendAllowed(true);
        writer.setLineAggregator(createLineAggregator(FlatFileConstants.PR01_CSV_FIELD_NAMES, FlatFileConstants.PR01_LINE_AGGREGATOR_FORMAT));
        writer.setFooterCallback(footerCallback);
        writer.setHeaderCallback(headerCallback);
        return writer;
    }

    @Bean(name = "FR01FlatFileItemWriter")
    public FlatFileItemWriter<FR01BatchOutput> fr01FlatFileItemWriter(TestFileProperties testFileProperties, FlatFileFooterCallback footerCallback, FlatFileHeaderCallback headerCallback) {
        FlatFileItemWriter<FR01BatchOutput> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource(testFileProperties.getOutput().getResults()));
        writer.setAppendAllowed(true);
        writer.setLineAggregator(createLineAggregator(FlatFileConstants.FR01_CSV_FIELD_NAMES, FlatFileConstants.FR01_LINE_AGGREGATOR_FORMAT));
        writer.setFooterCallback(footerCallback);
        writer.setHeaderCallback(headerCallback);
        return writer;
    }

    @Bean(name = "MR01FlatFileItemWriter")
    public FlatFileItemWriter<MR01BatchOutput> mr01FlatFileItemWriter(TestFileProperties testFileProperties, FlatFileFooterCallback footerCallback, FlatFileHeaderCallback headerCallback) {
        FlatFileItemWriter<MR01BatchOutput> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource(testFileProperties.getOutput().getResults()));
        writer.setAppendAllowed(true);
        writer.setLineAggregator(createLineAggregator(FlatFileConstants.MR01_CSV_FIELD_NAMES, FlatFileConstants.MR01_LINE_AGGREGATOR_FORMAT));
        writer.setFooterCallback(footerCallback);
        writer.setHeaderCallback(headerCallback);
        return writer;
    }

    @Bean(name = "DP01FlatFileItemWriter")
    public FlatFileItemWriter<DP01BatchOutput> dp01FlatFileItemWriter(TestFileProperties testFileProperties, FlatFileFooterCallback footerCallback, FlatFileHeaderCallback headerCallback) {
        FlatFileItemWriter<DP01BatchOutput> writer = new FlatFileItemWriter<>();
        writer.setResource(new FileSystemResource(testFileProperties.getOutput().getResults()));
        writer.setAppendAllowed(true);
        writer.setLineAggregator(createLineAggregator(FlatFileConstants.DP01_CSV_FIELD_NAMES, FlatFileConstants.DP01_LINE_AGGREGATOR_FORMAT));
        writer.setFooterCallback(footerCallback);
        writer.setHeaderCallback(headerCallback);
        return writer;
    }

    @Bean(name = "ClassifierCompositeItemWriter")
    public ClassifierCompositeItemWriter<BatchOutput> classifierCompositeItemWriter(SubclassClassifier subclassClassifier) {
        ClassifierCompositeItemWriter<BatchOutput> writer = new ClassifierCompositeItemWriter<>();
        writer.setClassifier(subclassClassifier);
        return writer;
    }

    @Bean
    public SubclassClassifier<Object, Object> subclassClassifier(
            FlatFileItemWriter<NP01BatchOutput> np01FlatFileItemWriter, FlatFileItemWriter<FP01BatchOutput> fp01FlatFileItemWriter,
            FlatFileItemWriter<SJ01BatchOutput> sj01FlatFileItemWriter, FlatFileItemWriter<PR01BatchOutput> pr01FlatFileItemWriter,
            FlatFileItemWriter<FR01BatchOutput> fr01FlatFileItemWriter, FlatFileItemWriter<MR01BatchOutput> mr01FlatFileItemWriter,
            FlatFileItemWriter<DP01BatchOutput> dp01FlatFileItemWriter) {
        SubclassClassifier<Object, Object> classifier = new SubclassClassifier<>();
        HashMap<Class<?>, Object> typeMap = new HashMap<>();
        typeMap.put(NP01BatchOutput.class, np01FlatFileItemWriter);
        typeMap.put(FP01BatchOutput.class, fp01FlatFileItemWriter);
        typeMap.put(SJ01BatchOutput.class, sj01FlatFileItemWriter);
        typeMap.put(PR01BatchOutput.class, pr01FlatFileItemWriter);
        typeMap.put(FR01BatchOutput.class, fr01FlatFileItemWriter);
        typeMap.put(MR01BatchOutput.class, mr01FlatFileItemWriter);
        typeMap.put(DP01BatchOutput.class, dp01FlatFileItemWriter);
        classifier.setTypeMap(typeMap);
        return classifier;
    }


    @Bean(name = "createFlatFileStep")
    public Step documentDeliveryStep(StepBuilderFactory stepBuilderFactory,
                                     @Qualifier("electronicDocumentsQueueItemReader") ItemReader<Message> reader,
                                     @Qualifier("electronicDocumentsQueueItemProcessor") ItemProcessor<Message, ElectronicDocumentsBatchOutput> itemProcessor,
                                     @Qualifier("electronicDocumentsQueueItemWriter") ItemWriter<ElectronicDocumentsBatchOutput> writer,
                                     FlatFileItemWriter<NP01BatchOutput> np01FlatFileItemWriter, FlatFileItemWriter<FP01BatchOutput> fp01FlatFileItemWriter,
                                     FlatFileItemWriter<SJ01BatchOutput> sj01FlatFileItemWriter, FlatFileItemWriter<PR01BatchOutput> pr01FlatFileItemWriter,
                                     FlatFileItemWriter<FR01BatchOutput> fr01FlatFileItemWriter, FlatFileItemWriter<MR01BatchOutput> mr01FlatFileItemWriter,
                                     FlatFileItemWriter<DP01BatchOutput> dp01FlatFileItemWriter) {
        return stepBuilderFactory.get("computeFlatFile")
                .<Message, ElectronicDocumentsBatchOutput>chunk(1)
                .reader(reader)
                .processor(itemProcessor)
                .writer(writer)
                .stream(np01FlatFileItemWriter)
                .stream(mr01FlatFileItemWriter)
                .stream(fp01FlatFileItemWriter)
                .stream(sj01FlatFileItemWriter)
                .stream(pr01FlatFileItemWriter)
                .stream(fr01FlatFileItemWriter)
                .stream(dp01FlatFileItemWriter)
                .build();
    }
}

我有一个包含多个 FlatFileItemWriters 的 ClassifierCompositeItemWriter。FlatFileItemWriters 处理不同的对象类型,并将单行输出到相同的平面文件中。截至目前,所有这些都有效。我需要添加的是批处理中单个输出文件的单个页眉和页脚。使用当前设置,我相信标头可以工作,尽管不像我希望的那样有意(从我注意到的情况来看,第一次调用 headerCallback 只会发生一次)。我当前的页脚设置多次调用页脚字符串,因为它在每个作者上调用 footerCallback,而不仅仅是 1。我想我可以手动执行此操作就好了,但我希望我能找到一个更相关的解决方案如果可能,弹簧配置。

总而言之,当从多个写入器写入单个文件时,有没有办法让 Spring Boot 自动配置平面文件的页眉和页脚行?

4

0 回答 0