java—在SpringBatch中,如何将处理过的文件移到另一个文件夹中,我使用的是multiresourceitemreader和chunk处理

vuktfyat  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(372)

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

rn0zuynd1#

我看不出在处理完每个文件后立即移动它的附加值,因为不管怎样,所有文件都将在该步骤之后移动。侦听器方法应该可以工作,但是windows上存在一个开放的问题(https://github.com/spring-projects/spring-batch/issues/1500). 因此,我建议在主步骤之后使用单独的步骤来移动文件。为此,可以在该步骤中注入相同的资源,并在tasklet中移动文件。

相关问题