Search in sources :

Example 1 with TNonblockingSocket

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")));
    }
}
Also used : TNonblockingSocket(org.apache.thrift.transport.TNonblockingSocket) TAsyncClientManager(org.apache.thrift.async.TAsyncClientManager) TimeoutException(java.util.concurrent.TimeoutException) TException(org.apache.thrift.TException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CompletableFuture(java.util.concurrent.CompletableFuture) RemoteMap(net.morimekta.test.providence.thrift.map.RemoteMap) Test(org.junit.Test)

Example 2 with TNonblockingSocket

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);
}
Also used : TException(org.apache.thrift.TException) TNonblockingSocket(org.apache.thrift.transport.TNonblockingSocket) TServer(org.apache.thrift.server.TServer) InetSocketAddress(java.net.InetSocketAddress) LoggerFactory(org.slf4j.LoggerFactory) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TAsyncClientManager(org.apache.thrift.async.TAsyncClientManager) THsHaServer(org.apache.thrift.server.THsHaServer) AsyncMethodCallback(org.apache.thrift.async.AsyncMethodCallback) TException(org.apache.thrift.TException) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) Test(org.junit.Test)

Example 3 with TNonblockingSocket

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);
        }
    }
}
Also used : SocketAddressFieldAccessor(com.navercorp.pinpoint.plugin.thrift.field.accessor.SocketAddressFieldAccessor) SocketFieldAccessor(com.navercorp.pinpoint.plugin.thrift.field.accessor.SocketFieldAccessor) SocketAddress(java.net.SocketAddress) Socket(java.net.Socket) TNonblockingSocket(org.apache.thrift.transport.TNonblockingSocket)

Example 4 with TNonblockingSocket

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);
}
Also used : TNonblockingSocket(org.apache.thrift.transport.TNonblockingSocket) TFramedTransport(org.apache.thrift.transport.TFramedTransport) SSLSocket(javax.net.ssl.SSLSocket) TTransportException(org.apache.thrift.transport.TTransportException) IOException(java.io.IOException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) TSocket(org.apache.thrift.transport.TSocket) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with TNonblockingSocket

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;
}
Also used : TNonblockingSocket(org.apache.thrift.transport.TNonblockingSocket)

Aggregations

TNonblockingSocket (org.apache.thrift.transport.TNonblockingSocket)6 IOException (java.io.IOException)2 Socket (java.net.Socket)2 TException (org.apache.thrift.TException)2 TAsyncClientManager (org.apache.thrift.async.TAsyncClientManager)2 TNonblockingServerSocket (org.apache.thrift.transport.TNonblockingServerSocket)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 SocketAddressFieldAccessor (com.navercorp.pinpoint.plugin.thrift.field.accessor.SocketAddressFieldAccessor)1 SocketFieldAccessor (com.navercorp.pinpoint.plugin.thrift.field.accessor.SocketFieldAccessor)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 SocketException (java.net.SocketException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 SSLSocket (javax.net.ssl.SSLSocket)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 RemoteMap (net.morimekta.test.providence.thrift.map.RemoteMap)1 AsyncMethodCallback (org.apache.thrift.async.AsyncMethodCallback)1