Last active
July 26, 2017 00:47
-
-
Save imasahiro/765c306d489ee10d65903486fbc75164 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private final class Handler implements HelloService.AsyncIface { | |
@Override | |
public void hello(String name, AsyncMethodCallback resultHandler) throws TException { | |
setAsyncResult(resultHandler, CompletableFuture.completedFuture("hello")); | |
} | |
} | |
public static <T> void setAsyncResult(AsyncMethodCallback<T> resultHandler, | |
ListenableFuture<T> future) { | |
Futures.addCallback(future, new FutureCallback<T>() { | |
@Override | |
public void onSuccess(@Nullable T result) { | |
resultHandler.onComplete(result); | |
} | |
@Override | |
public void onFailure(Throwable t) { | |
if (t instanceof Exception) { | |
resultHandler.onError((Exception) t); | |
} | |
} | |
}); | |
} | |
public static <T> void setAsyncResult(AsyncMethodCallback<T> resultHandler, | |
CompletableFuture<T> future) { | |
future.handle(voidFunction((result, thrown) -> { | |
if (thrown != null) { | |
resultHandler.onError((Exception) thrown); | |
} else { | |
resultHandler.onComplete(result); | |
} | |
})); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Server side | |
private final class HelloServiceHandler implements HelloService.AsyncIface { | |
@Override | |
public void hello(String name, AsyncMethodCallback resultHandler) throws TException { | |
resultHandler.onComplete(name); | |
} | |
} | |
private void setup() { | |
new ServerBuilder() | |
.port(8080, SessionProtocol.HTTP) | |
.serviceAt("/thrift", THttpService.of(new HelloServiceHandler())) | |
.build() | |
.start() | |
.join(); | |
} | |
// Client side | |
public CompletableFuture<String> hello(String name) throws TException { | |
HelloService.AsyncIface async = Clients.newClient("http+tbinary://127.0.0.1:8080/thrift", | |
HelloService.AsyncIface.class); | |
ThriftCompletableFuture<String> future = new ThriftCompletableFuture<>(); | |
async.hello(name, future); | |
return future; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment