英文:
Propagating information in completablefuture failure
问题
I'm using completable futures to do a bunch of thing X. X talks to the internet and can either fail or not fail. When I invoke X I pass it a value, let's call it value. X(value).
private void X(String value) {
    CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> {
        try {
           Object response = talkToExternalThing(value);
        } catch (InterruptedException e) {
            throw new CompletionException(e.getCause());
        }
        return true;
    }).exceptionally(ex -> false);
    futures.add(future);
}
Above is a snippet of what I'm playing with. When it comes down to analyzing the result-set, I can see all values that failed/didn't fail in my test (i.e., true or false).
Map<Boolean, List<CompletableFuture<Boolean>>> result = futures.stream()
 .collect(Collectors.partitioningBy(CompletableFuture::isCompletedExceptionally));
My problem is, I want to not only know if it failed or did not fail, but I also want other metadata, such as the value that caused the failure. My hope is to potentially have an exception object I can analyze as a result. It's worth noting that the exception is a checked exception (interrupt).
英文:
I'm using completable futures to do a bunch of thing X. X talks to the internet and can either fail or not fail. When I invoke X I pass it a value, let's call it value. X(value).
    private void X(String value) {
        CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(()-> {
            try {
               Object response = talkToExternalThing(value);
            } catch (InterruptedException e) {
                throw new CompletionException(e.getCause());
            }
            return true;
        }).exceptionally(ex -> false);
        futures.add(future);
    }
Above is a snippet of what I'm playing with. When it comes down to analyzing the result-set, I can see all values that failed/didn't fail in my test (ie. true or false).
Map<Boolean, List<CompletableFuture<Boolean>>> result = futures.stream()
 .collect(Collectors.partitioningBy(CompletableFuture::isCompletedExceptionally));
My problem is, I want to not only know if it failed or did not fail, but I also want other metadata, such as the value that caused the failure. My hope is to potentially have an exception object I can analyze as a result. It's worth noting that the exception is a checked exception (interrupt).
答案1
得分: 2
以下是翻译好的内容:
这将是我的建议:
ExecutorService executorService = Executors.newCachedThreadPool();
private void X(String value) {
    CompletableFuture<Pair<Boolean, String>> future = new CompletableFuture<>();
    executorService.execute(() -> {
        try {
            Object response = talkToExternalThing(value);
        } catch (InterruptedException e) {
            // 未成功,使用该值
            future.complete(new Pair<>(false, value));
            return;
        }
        // 成功,使用该值
        future.complete(new Pair<>(true, value));
    });
    futures.add(future);
}
希望这对你有帮助!
英文:
This would be my suggestion:
ExecutorService executorService = Executors.newCachedThreadPool();
private void X(String value) {
    CompletableFuture<Pair<Boolean, String>> future = new CompletableFuture<>();
    executorService.execute(() -> {
        try {
            Object response = talkToExternalThing(value);
        } catch (InterruptedException e) {
            // un-successfully, with the value
            future.complete(new Pair<>(false, value));
            return;
        }
        // successfully, with the value
        future.complete(new Pair<>(true, value));
    });
    
    futures.add(future);
    
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论