【问题标题】:spring batch itemWriter only write after get all itemsspring batch itemWriter 仅在获取所有项目后写入
【发布时间】:2017-08-31 20:09:35
【问题描述】:

我有一个简单的 Spring Batch 作业,有几个步骤,最后一步是编写报告,所以我有 ItemReader、ItemProcessor 和 ItemWriter。 ItemWriter 按块写入取决​​于步骤中定义的块编号,但我需要等到获取所有项目然后写入最终报告。我怎样才能做到这一点?

public class ReportItemWriter implements ItemWriter<ReportItem> {
    private List<ReportItem> allItems = Lists.newArrayList();
    ... 

public void write(List<? extends ReportItem> reportItems) throws Exception {
    allItems.addAll(reportItems);
    writeReport(allItems);  <-- here it only write chunk of items to the report
}

【问题讨论】:

  • 报告中需要哪些信息?你确定不能逐块合成吗?
  • 您可以在 close() 方法中实现 ItemStreamWriter 并构建写入最终报告。写入所有项目时将调用该方法。如果您需要更多详细信息,例如代码,请告诉我...

标签: java spring-batch


【解决方案1】:

如果你的作家有@JobScope。您可以使用@PreDestroy(Spring 注释)来实现这一点。 您可以拥有存储所有需要写入的项目的字段。在预销毁功能上,你自己写吧。

private List<YourItem> items = new ArrayList<>();

@Override
public void write(argument here) {
    items.addAll(YourItem);
}    

@PreDestroy
public void teardown() {
    // do the write for all items
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-15
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多