如何为每次运行的作业初始化MultiResourceItemReader。当前使用此设置,其每次运行作业仍使用相同的实例
我将@StepScope仍旧使用已处理过的文件的旧列表。我不确定在此代码中还需要添加什么
我还尝试了@JobScope,但也没有成功。我缺少一些基本的东西
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Value("file:ftp-inbound/*.csv")
@Autowired
private Resource[] inputResources;
@Autowired
private StepBuilderFactory steps;
@Autowired
private JobBuilderFactory jobs;
@Autowired
private ResourceLoader resourceLoader;
@Bean
public FlatFileItemReader<AccommodationRoomAvailability> itemReader() throws UnexpectedInputException, ParseException, IOException {
FlatFileItemReader<AccommodationRoomAvailability> reader = new FlatFileItemReader<AccommodationRoomAvailability>();
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
String[] tokens = {"Product ID", "Allotment", "Kamertype", "Zoeknaam", "Hotel", "Datum", "Beschikbaar", "Nachten"};
tokenizer.setNames(tokens);
tokenizer.setDelimiter(";");
tokenizer.setStrict(true);
reader.setLinesToSkip(1);
DefaultLineMapper<AccommodationRoomAvailability> lineMapper = new DefaultLineMapper<AccommodationRoomAvailability>();
lineMapper.setLineTokenizer(tokenizer);
lineMapper.setFieldSetMapper(new RecordFieldSetMapper());
reader.setLineMapper(lineMapper);
return reader;
}
@Bean
@Qualifier("multiResourceReader")
@StepScope
public MultiResourceItemReader<AccommodationRoomAvailability> multiResourceItemReader() throws Exception {
MultiResourceItemReader<AccommodationRoomAvailability> resourceItemReader = new MultiResourceItemReader<AccommodationRoomAvailability>();
resourceItemReader.setResources(inputResources);
resourceItemReader.setDelegate(itemReader());
resourceItemReader.setStrict(false);
resourceItemReader.setSaveState(false);
// resourceItemReader.read();
return resourceItemReader;
}
@Bean
public ItemProcessor<AccommodationRoomAvailability, String> itemProcessor() {
return new AvailabilityProcessor();
}
@Bean
public ItemWriter itemWriter() {
return new ItemWriter() {
@Override
public void write(List list) throws Exception {
}
};
}
@Bean
protected Step step1(@Qualifier("multiResourceReader") MultiResourceItemReader<AccommodationRoomAvailability> reader, ItemProcessor<AccommodationRoomAvailability, String> processor,
ItemWriter writer) {
return steps.get("step1")/*.listener(new StepListener())*/.<AccommodationRoomAvailability, String>chunk(30000).reader(reader)
.processor(processor)
.writer(writer)
.build();
}
@Bean
public Step step2() throws IOException {
FileDeletingTasklet task = new FileDeletingTasklet();
task.setResources(inputResources);
return stepBuilderFactory.get("step2")
.tasklet(task)
.build();
}
@Bean(name = "job")
public Job job(@Qualifier("step1") Step step1, Step step2) throws IOException {
return jobs.get("job")
.start(step1).on("*").to(step2).end()
// .flow(step1).on("").to(step2()).end()
.build();
}
}
答案 0 :(得分:0)
一旦创建了应用程序上下文,注入的资源@Value("file:ftp-inbound/*.csv")
在应用程序的整个生命周期中都是相同的。这就是为什么读者将始终读取相同的值的原因。
您需要将这些资源作为参数传递给您的工作,并将它们后期绑定到您的阅读器中,并具有Step
范围。在您的示例中,它将类似于:
@Bean
@Qualifier("multiResourceReader")
@StepScope
public MultiResourceItemReader<AccommodationRoomAvailability> multiResourceItemReader(@Value("#{jobParameters['inputResources']}") Resource[] inputResources) throws Exception {
MultiResourceItemReader<AccommodationRoomAvailability> resourceItemReader = new MultiResourceItemReader<AccommodationRoomAvailability>();
resourceItemReader.setResources(inputResources);
resourceItemReader.setDelegate(itemReader());
resourceItemReader.setStrict(false);
resourceItemReader.setSaveState(false);
return resourceItemReader;
}
然后将输入资源作为参数传递给您的工作:
JobParameters jobParameters = new JobParametersBuilder()
.addString("inputResources", "file:ftp-inbound/*.csv")
.toJobParameters();
当前使用此设置,它仍然为每个作业运行使用相同的实例
这是因为当您将资源注入配置类的字段中时,它们总是相同的。如果您使用我在上一个示例中提到的作业参数方法,那么使用不同的文件集运行作业将有不同的实例。