CompletableFuture in loop: How to collect failed records

41 Views Asked by At

I am using springboot and uploading an excel sheet to process which runs as an async job, I have the below method in the controller

CompletableFuture<List<String>> completableFutureErrorsList = importService.processExcelFile(worksheet);

The service has the async job, I am trying to email the combined errors as a list after each failed rows.

@Async
public CompletableFuture<List<String>> processExcelFile(XSSFSheet worksheet)
        throws IOException {
    List<String> errorsList = new ArrayList<>();
    for (int index = 2; index < worksheet.getPhysicalNumberOfRows(); index++) {
        XSSFRow row = worksheet.getRow(index);
        try {
            processExcelRow(row, errorsList);
        } catch (Exception e) {
            logger.error("Failed to save the record");
        }
    }
    return CompletableFuture.completedFuture(errorsList);
 }


  public void processExcelRow(XSSFRow row, List<String> errorsList) {
  XSSFCell cell = row.getCell(0, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
  if (cell == null) {
     errorsList.add("Missing Name");
     CompletableFuture.failedFuture(new Exception("Missing Name"));
  } else { //process
 }
}

How would I get a combined list at the end of the asych job, This asych job works fine I just need to get the combined list of errors at the end of the process, How would I achieve that? thanks in advance

0

There are 0 best solutions below