1
votes

I have a simple Spring Batch job with couple of steps, the last step is to write report, so i haver ItemReader, ItemProcessor and ItemWriter. ItemWriter write by chunk depends the chunk number defined in the step, but I need to wait until get all items then write final report. how can I do that?

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
Which kind of informations you need in your report? Are you sure you can't compose it chunk-by-chunk?Luca Basso Ricci
You can implement ItemStreamWriter and build write final report in close() method. That method will be called when all items are written. Please let me know if you need more detail like code ...Nghia Do

1 Answers

0
votes

If you have @JobScope on your writer. You can use @PreDestroy (Spring annotation) to achieve this. You can have field to store all of the items needed to be write. On predestroy function, you do your writing.

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
}