use of org.apache.thrift.async.TAsyncClientManager in project providence by morimekta.
the class NonblockingSocketServerTest method testWithNonblockingThriftClient.
@Test
public void testWithNonblockingThriftClient() throws IOException, TException, ExecutionException, InterruptedException, TimeoutException {
CompletableFuture<Boolean> a = new CompletableFuture<>();
CompletableFuture<Boolean> b = new CompletableFuture<>();
TAsyncClientManager manager = new TAsyncClientManager();
try (TNonblockingSocket socket_a = new TNonblockingSocket("localhost", port);
TNonblockingSocket socket_b = new TNonblockingSocket("localhost", port)) {
socket_a.startConnect();
socket_a.finishConnect();
socket_b.startConnect();
socket_b.finishConnect();
net.morimekta.test.thrift.thrift.map.RemoteMap.AsyncClient client_a = new net.morimekta.test.thrift.thrift.map.RemoteMap.AsyncClient(factory, manager, socket_a);
client_a.put("a", "bb", new AsyncMethodCallback<Boolean>() {
@Override
public void onComplete(Boolean response) {
a.complete(response);
}
@Override
public void onError(Exception exception) {
a.completeExceptionally(exception);
}
});
// Since thrift clients does not support handling multiple requests at
// the same time (yes, even the AsyncClient does not support parallel
// execution).
net.morimekta.test.thrift.thrift.map.RemoteMap.AsyncClient client_b = new net.morimekta.test.thrift.thrift.map.RemoteMap.AsyncClient(factory, manager, socket_b);
client_b.put("b", "aaa", new AsyncMethodCallback<Boolean>() {
@Override
public void onComplete(Boolean response) {
b.complete(response);
}
@Override
public void onError(Exception exception) {
b.completeExceptionally(exception);
}
});
assertThat(a.get(1, TimeUnit.SECONDS), is(Boolean.FALSE));
assertThat(b.get(1, TimeUnit.SECONDS), is(Boolean.FALSE));
assertThat(remoteMap, is(ImmutableMap.of("a", "bb", "b", "aaa")));
}
}
use of org.apache.thrift.async.TAsyncClientManager in project commons by twitter.
the class ThriftFactory method createAsyncClientFactory.
private <T> Function<TTransport, T> createAsyncClientFactory(final Class<T> serviceInterface) throws IOException {
final TAsyncClientManager clientManager = new TAsyncClientManager();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
clientManager.stop();
}
});
final Constructor<? extends T> implementationConstructor = findAsyncImplementationConstructor(serviceInterface);
return new Function<TTransport, T>() {
@Override
public T apply(TTransport transport) {
Preconditions.checkNotNull(transport);
Preconditions.checkArgument(transport instanceof TNonblockingTransport, "Invalid transport provided to client factory: " + transport.getClass());
try {
T client = implementationConstructor.newInstance(new TBinaryProtocol.Factory(), clientManager, transport);
if (socketTimeout != null) {
((TAsyncClient) client).setTimeout(socketTimeout.as(Time.MILLISECONDS));
}
return client;
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
};
}
use of org.apache.thrift.async.TAsyncClientManager in project mlib by myshzzx.
the class AsyncTest1 method test2.
@Test
public void test2() throws Exception {
TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(new InetSocketAddress("l", 19000), 0);
TServer server = new THsHaServer(new THsHaServer.Args(serverTransport).processor(new TService1.AsyncProcessor<TService1.AsyncIface>(new Service2Impl())).protocolFactory(new TCompactProtocol.Factory()));
new Thread() {
@Override
public void run() {
server.serve();
}
}.start();
Thread.sleep(1000);
TNonblockingSocket transport = new TNonblockingSocket("l", 19000, 5000);
TService1.AsyncIface client = new TService1.AsyncClient(new TCompactProtocol.Factory(), new TAsyncClientManager(), transport);
byte[] b = { 1, 2, 3 };
AsyncMethodCallback<TService1.AsyncClient.getStr_call> h = new AsyncMethodCallback<TService1.AsyncClient.getStr_call>() {
@Override
public void onComplete(TService1.AsyncClient.getStr_call response) {
try {
System.out.println(response.getResult());
} catch (TException e) {
log.error("async get rsp fail.", e);
}
}
@Override
public void onError(Exception e) {
log.error("async call fail.", e);
}
};
for (int i = 0; i < 2; i++) {
client.getStr("mysh", ByteBuffer.wrap(b), h);
}
Thread.sleep(10000000);
}
Aggregations