Win a copy of Penetration Testing Basics this week in the Security forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CompletableFuture and exceptions

Toby Eggitt
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was tinkering with the CompletableFuture stuff recently, and came across a question/situation I failed to resolve.

One of the neat things this API provides for is to have two operations running, and to take the output of whichever completes first (e.g. two web service requests of different servers). However, if the first to complete does so because it fails and throws an exception, I seem to be faced with a choice. If I don't do a handle... / handleAsync... to catch the exception in the pipeline, then I seem to kill the "whole thing" (though I admit that at this point, I'm not totally clear where the exception goes and if, perhaps, there's a way to make it quietly disappear). On the other hand, if I do perform the handle operation, then it seems that if the failure happens first, then the output of the handle operation seems to be unavoidably used for the downstream processing, which is pretty counterproductive. If I do the combine... rather than either... operation, then I end up waiting for both to continue, which is also silly.

What did I miss? How can I do this in a way that the first task to finish gets used, but only provided it ran successfully.

I guess, as part of this, or a possible solution, or another general question, is there a way to quietly terminate one of these asynchronous "pipeline" type things based on some decision logic, rather than have it continue?

Also, in general, does anyone have any good resources on this API: good books, blogs, examples, or whatever? I don't think there's anything in the Java Tutorial series on this yet, is there?

  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic