CompletableFuture.stopIf

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

CompletableFuture.stopIf

federico.fissore@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
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: CompletableFuture.stopIf

Dávid Karnok
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 [hidden email] <[hidden email]>:
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
[hidden email]
http://cs.oswego.edu/mailman/listinfo/concurrency-interest



--
Best regards,
David Karnok

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

Re: CompletableFuture.stopIf

federico.fissore@gmail.com
Dávid Karnok ha scritto il 02/09/2016 alle 13:15:

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

Ah that's a nice syntax. Thank you. I didn't know about reactor-core

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