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