What is the ideal way to handle two different types of futures where one future is dependent on the other future?
I'm looking at AKKA's Java Futures API, and I see a lot of ways to handle multiple futures of the same type, but I don't see anything that pops out at me for handling futures of different types. I开发者_如何学JAVA'm guessing that I'm making this more complicated than it is.
Anyways let's say we have two different Actors: actorA and actorB. They are based on different classes and they each return a different Future. However, actorB is dependent on a Future from actorA. Is the following logic, how to best handle this case?
Future<A> a = actorA.sendRequestReplyFuture(...);
Future<B> b = actorB.sendRequestReplyFuture(a);
How about if we have a list of actorAs and actorBs?
WARNING, just written from memory, might not compile OOTB:
Future<B> b = actor.sendRequestReplyFuture().flatMap( new akka.japi.Function<A,Future<B>>() {
public Future<B> apply(A a) {
return actor.sendRequestReplyFuture(a);
}
}
Why not just wrap them, as you have, but with one slight change - pass the result of a into b.
Future a = actorA.sendRequestReplyFuture(...);
Future b = actorB.sendRequestReplyFuture(a.get());
This arrangement just means that actorB.sendRequestReplyFuture()
won't get called until actorA.sendRequestReplyFuture()
has finished. Nothing wrong with that, in fact, I like the pattern you've got there.
精彩评论