在CompletableFuture失败中传播信息

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

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).

  1. private void X(String value) {
  2. CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> {
  3. try {
  4. Object response = talkToExternalThing(value);
  5. } catch (InterruptedException e) {
  6. throw new CompletionException(e.getCause());
  7. }
  8. return true;
  9. }).exceptionally(ex -> false);
  10. futures.add(future);
  11. }

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).

  1. Map<Boolean, List<CompletableFuture<Boolean>>> result = futures.stream()
  2. .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).

  1. private void X(String value) {
  2. CompletableFuture&lt;Boolean&gt; future = CompletableFuture.supplyAsync(()-&gt; {
  3. try {
  4. Object response = talkToExternalThing(value);
  5. } catch (InterruptedException e) {
  6. throw new CompletionException(e.getCause());
  7. }
  8. return true;
  9. }).exceptionally(ex -&gt; false);
  10. futures.add(future);
  11. }

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).

  1. Map&lt;Boolean, List&lt;CompletableFuture&lt;Boolean&gt;&gt;&gt; result = futures.stream()
  2. .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

以下是翻译好的内容:

这将是我的建议:

  1. ExecutorService executorService = Executors.newCachedThreadPool();
  2. private void X(String value) {
  3. CompletableFuture<Pair<Boolean, String>> future = new CompletableFuture<>();
  4. executorService.execute(() -> {
  5. try {
  6. Object response = talkToExternalThing(value);
  7. } catch (InterruptedException e) {
  8. // 未成功,使用该值
  9. future.complete(new Pair<>(false, value));
  10. return;
  11. }
  12. // 成功,使用该值
  13. future.complete(new Pair<>(true, value));
  14. });
  15. futures.add(future);
  16. }

希望这对你有帮助!

英文:

This would be my suggestion:

  1. ExecutorService executorService = Executors.newCachedThreadPool();
  2. private void X(String value) {
  3. CompletableFuture&lt;Pair&lt;Boolean, String&gt;&gt; future = new CompletableFuture&lt;&gt;();
  4. executorService.execute(() -&gt; {
  5. try {
  6. Object response = talkToExternalThing(value);
  7. } catch (InterruptedException e) {
  8. // un-successfully, with the value
  9. future.complete(new Pair&lt;&gt;(false, value));
  10. return;
  11. }
  12. // successfully, with the value
  13. future.complete(new Pair&lt;&gt;(true, value));
  14. });
  15. futures.add(future);
  16. }

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:

确定