Search in sources :

Example 1 with TAsyncClient

use of org.apache.thrift.async.TAsyncClient 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)

Aggregations

Function (com.google.common.base.Function)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 TAsyncClient (org.apache.thrift.async.TAsyncClient)1 TAsyncClientManager (org.apache.thrift.async.TAsyncClientManager)1 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)1 TNonblockingTransport (org.apache.thrift.transport.TNonblockingTransport)1 TTransport (org.apache.thrift.transport.TTransport)1