use of org.apache.thrift.transport.TNonblockingSocket 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.transport.TNonblockingSocket 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);
}
use of org.apache.thrift.transport.TNonblockingSocket in project pinpoint by naver.
the class TNonblockingSocketConstructInterceptor method after.
@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
if (isDebug) {
logger.afterInterceptor(target, args, result, throwable);
}
if (validate(target, args)) {
Socket socket = ((TNonblockingSocket) target).getSocketChannel().socket();
((SocketFieldAccessor) target)._$PINPOINT$_setSocket(socket);
if (args[2] instanceof SocketAddress) {
SocketAddress socketAddress = (SocketAddress) args[2];
((SocketAddressFieldAccessor) target)._$PINPOINT$_setSocketAddress(socketAddress);
}
}
}
use of org.apache.thrift.transport.TNonblockingSocket in project commons by twitter.
the class ThriftConnectionFactory method createTransport.
@VisibleForTesting
TTransport createTransport(int timeoutMillis) throws TTransportException, IOException {
TSocket socket = null;
if (transportType != TransportType.NONBLOCKING) {
// can't do a nonblocking create on a blocking transport
if (timeoutMillis <= 0) {
return null;
}
if (sslTransport) {
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket ssl_socket = (SSLSocket) factory.createSocket(endpoint.getHostName(), endpoint.getPort());
ssl_socket.setSoTimeout(timeoutMillis);
return new TSocket(ssl_socket);
} else {
socket = new TSocket(endpoint.getHostName(), endpoint.getPort(), timeoutMillis);
}
}
try {
switch(transportType) {
case BLOCKING:
socket.open();
setSocketTimeout(socket);
return socket;
case FRAMED:
TFramedTransport transport = new TFramedTransport(socket);
transport.open();
setSocketTimeout(socket);
return transport;
case NONBLOCKING:
try {
return new TNonblockingSocket(endpoint.getHostName(), endpoint.getPort());
} catch (IOException e) {
throw new IOException("Failed to create non-blocking transport to " + endpoint, e);
}
}
} catch (TTransportException e) {
throw new TTransportException("Failed to create transport to " + endpoint, e);
}
throw new IllegalArgumentException("unknown transport type " + transportType);
}
use of org.apache.thrift.transport.TNonblockingSocket in project eiger by wlloyd.
the class CustomTNonBlockingServer method requestInvoke.
@Override
protected boolean requestInvoke(FrameBuffer frameBuffer) {
TNonblockingSocket socket = (TNonblockingSocket) frameBuffer.trans_;
SocketSessionManagementService.remoteSocket.set(socket.getSocketChannel().socket().getRemoteSocketAddress());
frameBuffer.invoke();
return true;
}
Aggregations