• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CompletableFuture and exceptions

 
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic