Код: Выделить всё
public void startProcess(List fooDetails) throws Exception {
List futures = fooDetails.stream()
.map(this::processFoo)
.collect(Collectors.toList());
List failedFooPushList = futures.stream()
.map(future -> {
try {
return future.get(); // Returns null if successful for that store
} catch (Exception e) {
return null;
}
})
.filter(Objects::nonNull) // Filter failed (non-null) results
.collect(Collectors.toList());
if (!failedFooPushList.isEmpty()) {
throw new SyncException("Foo failed for foo details: {}" + failedFooPushList);
}
}
private CompletableFuture processFoo(Foo fooDetail) {
return CompletableFuture.supplyAsync(() -> {
try {
Bar request = barService.createBarFromDB(
Collections.singletonList(foodetail));
boolean barPushFailed = booleanResponseClient.updateBar(request);
if (barPushFailed) {
return fooDetail.getName();
}
return null; // Return null to indicate success
} catch (Exception ex) {
return fooDetail.getName();
}
});
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... tions-only
Мобильная версия