【发布时间】:2016-07-19 09:24:10
【问题描述】:
我们有一个 Spring Batch 作业,它从文件中提取收件人的动态列表。我们想添加一个额外的收件人作为质量控制。我考虑添加一个新的 tasklet,它只是吐出这条记录并将其传递给真正的读者。我在这里阅读了一些问题、其他地方的文章以及有关在 Spring Batch 步骤之间传输数据的文档,但我不确定这是完成此任务的最简单或最好的方法。
就像official documentation 使用监听器,this article 使用自动装配组件和不同监听器,以及this question and answers。
如果我确实设置了生成器 tasklet 并将其数据传递给阅读器,我将如何将其插入阅读器的实际记录中?
我们正在使用的代码的一些 sn-ps - 它纯粹是注释驱动的,没有任何 XML 配置设置。
步骤生成器
public Step loadRecipients() {
return stepBuilderFactory.get("loadRecipients").<Recipient, Recipient>chunk(chunkSize)
.reader(recipientsItemReader)
.processor(recipientsItemProcessor)
.writer(recipientsWriter)
.taskExecutor(taskExecutor)
.throttleLimit(1)
.build();
}
阅读器配置
@StepScope
public FlatFileItemReader<Recipient> recipientItemReader() {
FlatFileItemReader<Recipient> itemReader = new FilePrefixItemReader<>(
"theFilePath",
staticResourceLoader(),
FunctionUtils.propagateExceptions((org.springframework.core.io.Resource resource) -> new GZIPInputStream(resource.getInputStream()))
);
userCategoryItemReader.setLineMapper(userCategoriesDefaultLineMapper);
return userCategoryItemReader;
}
我是否应该使用一些时髦的包装器将我的额外记录添加到资源输入流中?我可以使用其他一些 Spring 魔法来添加我的静态记录吗?
【问题讨论】:
标签: java spring spring-batch