spring batch itemWriter只在获取所有项目后写入

时间:2017-04-06 01:37:34

标签: java spring-batch

我有一个简单的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
}

1 个答案:

答案 0 :(得分:0)

如果您的作家上有@JobScope。您可以使用@PreDestroy(Spring注释)来实现此目的。 您可以具有用于存储所有需要写入的项目的字段。在predestroy功能上,您可以进行编写。

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
}