我正在尝试将 Spring Batch 2.2.5 与 Java 配置一起使用。这是我的配置:
@Configuration
@EnableBatchProcessing
public class JobConfiguration {
@Autowired
private JobBuilderFactory jobBuilder;
@Autowired
private StepBuilderFactory stepBuilder;
@Bean
@Autowired
public Job processDocumentsJob() {
return jobBuilder.get("processDocumentsJob")
.start(procesingStep())
.build();
}
@Bean
@Autowired
public Step procesingStep() {
CompositeItemProcessor<File, DocumentPackageFileMetadata> compositeProcessor = new CompositeItemProcessor<File, DocumentPackageFileMetadata>();
compositeProcessor.setDelegates(Lists.newArrayList(
documentPackageFileValidationProcessor(),
documentMetadataFileTransformer()
));
return stepBuilder.get("procesingStep")
.<File, DocumentPackageFileMetadata>chunk(1)
.reader(documentPackageFileReader())
.processor(compositeProcessor)
.writer(documentMetadataFileMigrator())
.faultTolerant()
.skip(DocumentImportException.class)
.skipLimit(10)
.listener(stepExecutionListener())
.build();
}
....
}
使用上面的配置,如果 itemwriter(documentMetadataFileMigrator 指向的 bean)抛出“DocumentImportException”,则不会跳过该异常。 Spring Batch 实际上会再次重试相同的输入。即它将对“documentPackageFileValidationProcessor”使用相同的输入。
但是,如果我将 itemwriter 内部的逻辑移动到 itemprocessor 中:
@Bean
@Autowired
public Step procesingStep() {
CompositeItemProcessor<File, DocumentPackageFileMetadata> compositeProcessor = new CompositeItemProcessor<File, DocumentPackageFileMetadata>();
compositeProcessor.setDelegates(Lists.newArrayList(
documentPackageFileValidationProcessor(),
documentMetadataFileTransformer(),
documentMetadataFileMigratorAsProcessor() // same as itemwriter, but implemented as itemprocessor
));
return stepBuilder.get("procesingStep")
.<File, DocumentPackageFileMetadata>chunk(1)
.reader(documentPackageFileReader())
.processor(compositeProcessor)
.faultTolerant()
.skip(DocumentImportException.class)
.skipLimit(10)
.listener(stepExecutionListener())
.build();
}
那么异常将被正确跳过。即 Spring Batch 不会针对“documentPackageFileValidationProcessor”重试相同的项目。它将转到下一个要处理的项目(从“documentPackageFileReader”返回的项目)。
这是 Spring Batch 上的错误,还是它的行为符合预期?如果是这样,有人可以指出我相关的文档吗?
谢谢大家,如果这是一个基本问题,请道歉。
此致,
Alex