在CompletableFuture失败中传播信息

huangapple go评论61阅读模式
英文:

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&lt;Boolean&gt; future = CompletableFuture.supplyAsync(()-&gt; {
            try {
               Object response = talkToExternalThing(value);
            } catch (InterruptedException e) {
                throw new CompletionException(e.getCause());
            }
            return true;
        }).exceptionally(ex -&gt; 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&lt;Boolean, List&lt;CompletableFuture&lt;Boolean&gt;&gt;&gt; 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&lt;Pair&lt;Boolean, String&gt;&gt; future = new CompletableFuture&lt;&gt;();
    executorService.execute(() -&gt; {
        try {
            Object response = talkToExternalThing(value);
        } catch (InterruptedException e) {
            // un-successfully, with the value
            future.complete(new Pair&lt;&gt;(false, value));
            return;
        }

        // successfully, with the value
        future.complete(new Pair&lt;&gt;(true, value));
    });
    
    futures.add(future);
    
}

huangapple
  • 本文由 发表于 2020年8月12日 07:15:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63367608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定