Search in sources :

Example 1 with TAsyncClientManager

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")));
    }
}
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 TAsyncClientManager

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);
            }
        }
    };
}
Also used : TAsyncClient(org.apache.thrift.async.TAsyncClient) TAsyncClientManager(org.apache.thrift.async.TAsyncClientManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) Function(com.google.common.base.Function) TNonblockingTransport(org.apache.thrift.transport.TNonblockingTransport) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TTransport(org.apache.thrift.transport.TTransport)

Example 3 with TAsyncClientManager

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);
}
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)

Aggregations

TAsyncClientManager (org.apache.thrift.async.TAsyncClientManager)3 TException (org.apache.thrift.TException)2 TNonblockingSocket (org.apache.thrift.transport.TNonblockingSocket)2 Test (org.junit.Test)2 Function (com.google.common.base.Function)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 InetSocketAddress (java.net.InetSocketAddress)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 RemoteMap (net.morimekta.test.providence.thrift.map.RemoteMap)1 AsyncMethodCallback (org.apache.thrift.async.AsyncMethodCallback)1 TAsyncClient (org.apache.thrift.async.TAsyncClient)1 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)1 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)1 THsHaServer (org.apache.thrift.server.THsHaServer)1 TServer (org.apache.thrift.server.TServer)1 TNonblockingServerSocket (org.apache.thrift.transport.TNonblockingServerSocket)1 TNonblockingTransport (org.apache.thrift.transport.TNonblockingTransport)1