Library with CompletableFuture utility methods

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Library with CompletableFuture utility methods

Florian Schoppmann
Hi all,

In light of previous related discussions of
CompletionStage/CompletableFuture on this list [1-4], there may be
interest in a very small library of mine that helps with several common
use cases that come up in practice (checked exceptions, combining
multiple futures, asynchronous try-with-resources, etc.).

  https://github.com/fschopp/java-futures

Available in Central, BSD license, virtually full code coverage,
extensive JavaDoc, and no dirty hacks :) -- hopefully of use for some
readers.

Florian


[1] "CompletableFuture",
http://cs.oswego.edu/pipermail/concurrency-interest/2012-December/010486.html
[2] "Layered exception handling with CompletableFuture",
http://cs.oswego.edu/pipermail/concurrency-interest/2014-August/012911.html
[3] "Candidate jdk9 CompletableFuture additions",
http://cs.oswego.edu/pipermail/concurrency-interest/2015-January/013600.html
[4] "CompletableFuture.whenComplete survey",
http://cs.oswego.edu/pipermail/concurrency-interest/2015-December/014779.html

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Library with CompletableFuture utility methods

Valentin Kovalenko
Finally we have a method that converts a collection of futures into a future of collection of results :) Thanks!

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Library with CompletableFuture utility methods

Christian Schudt
In reply to this post by Florian Schoppmann
Hi,

you might be interested in my attempt:
https://bitbucket.org/sco0ter/babbler/src/3dcb590b3c92f2ceb80a1498b623e035fb59c227/xmpp-core/src/main/java/rocks/xmpp/util/concurrent/CompletionStages.java?at=master&fileviewer=file-view-default

allOf() is very similar as your collect() I think (except that it also flatMaps).

I also found withFallback() useful (composing two stages and if the first one fails, executes the second one, and only if this fails, too, completes exceptionally).

I’ve got the feeling, that at least some of them could fit well in the JDK.
un/wrapInCompletionException is useful, too. I’ve used a few times as one liner.

— Christian


> Am 15.03.2016 um 00:47 schrieb Florian Schoppmann <[hidden email]>:
>
> Hi all,
>
> In light of previous related discussions of
> CompletionStage/CompletableFuture on this list [1-4], there may be
> interest in a very small library of mine that helps with several common
> use cases that come up in practice (checked exceptions, combining
> multiple futures, asynchronous try-with-resources, etc.).
>
>  https://github.com/fschopp/java-futures
>
> Available in Central, BSD license, virtually full code coverage,
> extensive JavaDoc, and no dirty hacks :) -- hopefully of use for some
> readers.
>
> Florian
>
>
> [1] "CompletableFuture",
> http://cs.oswego.edu/pipermail/concurrency-interest/2012-December/010486.html
> [2] "Layered exception handling with CompletableFuture",
> http://cs.oswego.edu/pipermail/concurrency-interest/2014-August/012911.html
> [3] "Candidate jdk9 CompletableFuture additions",
> http://cs.oswego.edu/pipermail/concurrency-interest/2015-January/013600.html
> [4] "CompletableFuture.whenComplete survey",
> http://cs.oswego.edu/pipermail/concurrency-interest/2015-December/014779.html
>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: Library with CompletableFuture utility methods

Florian Schoppmann
Christian Schudt <[hidden email]> wrote:

> you might be interested in my attempt:
> https://bitbucket.org/sco0ter/babbler/src/3dcb590b3c92f2ceb80a1498b623e035
> fb59c227/xmpp-core/src/main/java/rocks/xmpp/util/concurrent/CompletionStag
> es.java?at=master&fileviewer=file-view-default
>
> allOf() is very similar as your collect() I think (except that it also
> flatMaps).

I looked over your code briefly, allOf() is indeed equivalent in effect
to my Futures#collect().

The use of CompletableFuture#join() in your code may be considered
undesirable by some (including static code analyis) because it is a
blocking function -- even though it is guaranteed not to block in that
context.

In any case, the Futures class in project java-futures also provides
Futures#shortCircuitCollect(). This is similar to Akka's
Futures#sequence(), which also has short-circuit semantics:
<http://doc.akka.io/api/akka/2.4.2/index.html#akka.dispatch.Futures$@sequence[A](in:Iterable[scala.concurrent.Future[A]],executor:scala.concurrent.ExecutionContext):scala.concurrent.Future[Iterable[A]]>

> I also found withFallback() useful (composing two stages and if the first
> one fails, executes the second one, and only if this fails, too,
> completes exceptionally).

I don't see the use of this method, given
CompletionStage#exceptionally()?

> I've got the feeling, that at least some of them could fit well in the JDK.

Ditto. Though a few additions are already in the current JDK9 code base.

Florian

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest