在 spring 批处理中我们如何将处理过的文件移动到另一个文件夹,我正在使用 MultiResourceItemReader 和块处理

In spring batch how can we move the processed files to another folder, I am using MultiResourceItemReader and chunk processing

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

我有所有的批处理文件:

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

项目 reader 是:

    @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();
}

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