I'm working on a java method that receive a list of items, generate a report (PDF file) of each of them, and finally zip all these PDF files into a zip file.
This is my simplified method:
List<File> pdfs = new ArrayList<File>();
public void generateZipOfItems(List<Item> items) {
for(Item item : items) {
ReportThread report = generatePDFReport(item);
report.addListener(new ReportExecuteListener() {
@Override
public void finishReport(ReportExecutionEvent reportExecutionEvent) {
// when report is finished, add the PDF file on arrayList
pdfs.add(reportExecutionEvent.getReport());
}
}
createZipFromFileList(pdfs); // this method just zip the files, and it is working very well
}
In a perfect scenario, the method should generate the report of item A, add it on "pdfs" array and repeat for item B, C, and so on. Finally, with all pdfs on array, it should zip the file.
Problem is, the loop is not waiting for the listener to finish generating the report files, and the zip file is being created without items.
The loop should wait for the listener before iterate. How can I do that?