[concurrency-interest] CompletableFuture.stopIf

Dávid Karnok akarnokd at gmail.com
Fri Sep 2 07:15:47 EDT 2016


Have you considered using Reactor-Core (Java 8+) instead? Assuming your
methods return CompletableFuture

Mono.fromFuture(fetchUser(id))
.filter(user -> user != null)
.flatMap(user ->
    Mono.fromFuture(changeEmail(user))
    .then(Mono.fromFuture(notifyOtherSystemsOfNewEmail(user)))
)
.subscribe(v -> { }, e -> log.error(e));


2016-09-02 12:18 GMT+02:00 federico.fissore at gmail.com <
federico.fissore at gmail.com>:

> Hi all
>
> Is there still time to add a method to CompletableFuture?
>
> I would like to have a stopIf/interruptIf method that stops a chain of CFs
> when the given predicate is true
>
> Consider the following example:
>
> fetchUser(id)
>  - changeEmail(user)
>  - notifyOtherSystems(user)
>
> If fetchUser returns null, the following steps are nonsense. At the
> moment, you have to copy/paste a check for a null user in both changeEmail
> and notifyOtherSystems steps.
>
> What about:
>
> fetchUser(id)
>  - stopIf((user) -> user == null)
>  - changeEmail(user)
>  - notifyOtherSystemsOfNewEmail(user)
>
> stopIf exceptionally completes the CF with a CompletionException.
>
> Please note that fetchUser cannot complete exceptionally on its own, since
> it may be used elsewhere in the code base, where a null result has
> different semantics
>
> Best regards
>
> Federico Fissore
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>



-- 
Best regards,
David Karnok
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20160902/93097003/attachment.html>


More information about the Concurrency-interest mailing list