2

在 Spring 批处理应用程序中,我们如何在处理每个文件时将文件从 BatchFileDir 文件夹移动到目标文件夹。BatchFileDir 文件夹中会有一堆文件,所以我使用 MultiResourceItemReader。我也在关注基于块的处理。

我有所有的批处理文件:

    @Value("BatchFileDir/batchFile_*.csv")
    private Resource[] allBatchFiles;

项目阅读器是:

    @Bean
    public MultiResourceItemReader<MyServiceRequest> multiResourceItemReader() {
    MultiResourceItemReader<MyServiceRequest> resourceItemReader = new 
    MultiResourceItemReader<MyServiceRequest>();
    resourceItemReader.setResources(allBatchFiles);
    FlatFileItemReader<Record> reader = new FlatFileItemReader<>();
    DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
    String[] tokens = {"id", "name", "author", "subject"};
    tokenizer.setNames(tokens);
    DefaultLineMapper< MyServiceRequest> lineMapper = new DefaultLineMapper<>();
    lineMapper.setLineTokenizer(tokenizer);
    lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
    reader.setLinesToSkip(1);
    reader.setLineMapper(lineMapper);
    resourceItemReader.setDelegate(reader);
    return resourceItemReader;
}

并且项目处理器是:

    @Bean
    public ItemProcessor< MyServiceRequest, MyServiceResponse> itemProcessor() {
    return new ModifiedItemProcessor();
    }
   

ModifiedItemProcessor 是:

    public class ModifiedItemProcessor implements ItemProcessor< MyServiceRequest, 
    MyServiceResponse > {
    
    public MyServiceResponse process(MyServiceRequest item) {
    // interact with other Microservices and get the response
    return response;

步骤是:

       @Bean
       protected Step step(@Qualifier("itemProcessor") ItemProcessor<MyServiceRequest, 
       MyServiceResponse> processor, ItemWriter<MyServiceRequest> writer) {
       return stepBuilderFactory
            .get("myStep")
            .<MyServiceRequest, MyServiceResponse> chunk(99)
            .reader(multiResourceItemReader())
            .processor(processor)
            .writer(writer)
            .build();
}
4

1 回答 1

0

我看不到在处理每个文件后立即移动它的附加价值,因为无论如何,所有文件都会在该步骤之后移动。侦听器方法应该可以工作,但是 Windows 上有一个未解决的问题(https://github.com/spring-projects/spring-batch/issues/1500)。因此,我建议在主要步骤之后使用单独的步骤来移动文件。为此,您可以在该步骤中注入相同的资源并在 tasklet 中移动文件。

于 2020-12-07T19:48:24.317 回答