facebookarchive / swift

An annotation-based Java library for creating Thrift serializable types and services.
Apache License 2.0
900 stars 297 forks source link

Support for Future of Future for Async Services #271

Closed supercharger closed 9 years ago

supercharger commented 9 years ago

Let's say a Client is calling ServiceA which is in turn calling ServiceB. The method calls in ServiceA and ServiceB are Async by using ListenableFutures.

In the ServiceA method call "doSomeWorkA", I don't want to wait on the Future returned by ServiceB, so It won't impact the throughput. Please note that I need to construct ProcessedResultA from ProcessedResultB

Can you help on how to do this.

@ThriftService
public interface ServiceA  {
    // Calls ServiceB
   @ThriftMethod
    public ListenableFuture<ProcessedResultA> doSomeWorkA(String input);
}

@ThriftService
public interface ServiceB {
   @ThriftMethod
    public ListenableFuture<ProcessedResultB> doSomeWorkB(String input);
}
x1957 commented 9 years ago

You can use Futures.transform()

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/Futures.html#transform(com.google.common.util.concurrent.ListenableFuture,%20com.google.common.util.concurrent.AsyncFunction)

supercharger commented 9 years ago

I will try this, Thanks!!