Example usage for org.apache.thrift.async TAsyncClient setTimeout

List of usage examples for org.apache.thrift.async TAsyncClient setTimeout

Introduction

In this page you can find the example usage for org.apache.thrift.async TAsyncClient setTimeout.

Prototype

public void setTimeout(long timeout) 

Source Link

Usage

From source file:com.nearinfinity.blur.thrift.AsyncClientPool.java

License:Apache License

private TAsyncClient newClient(Class<?> c, Connection connection) throws InterruptedException {
    BlockingQueue<TAsyncClient> blockingQueue = getQueue(connection);
    TAsyncClient client = blockingQueue.poll();
    if (client != null) {
        return client;
    }//from w w  w. j  av a  2s.com

    AtomicInteger counter;
    synchronized (_numberOfConnections) {
        counter = _numberOfConnections.get(connection.getHost());
        if (counter == null) {
            counter = new AtomicInteger();
            _numberOfConnections.put(connection.getHost(), counter);
        }
    }

    synchronized (counter) {
        int numOfConnections = counter.get();
        while (numOfConnections >= _maxConnectionsPerHost) {
            client = blockingQueue.poll(_pollTime, TimeUnit.MILLISECONDS);
            if (client != null) {
                return client;
            }
            LOG.debug("Waiting for client number of connection [" + numOfConnections
                    + "], max connection per host [" + _maxConnectionsPerHost + "]");
            numOfConnections = counter.get();
        }
        LOG.info("Creating a new client for [" + connection + "]");
        String name = c.getName();
        Constructor<?> constructor = _constructorCache.get(name);
        if (constructor == null) {
            String clientClassName = name.replace("$AsyncIface", "$AsyncClient");
            try {
                Class<?> clazz = Class.forName(clientClassName);
                constructor = clazz.getConstructor(new Class[] { TProtocolFactory.class,
                        TAsyncClientManager.class, TNonblockingTransport.class });
                _constructorCache.put(name, constructor);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        try {
            TNonblockingSocket transport = newTransport(connection);
            client = (TAsyncClient) constructor
                    .newInstance(new Object[] { _protocolFactory, _clientManager, transport });
            client.setTimeout(_timeout);
            counter.incrementAndGet();
            return client;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}