Example usage for org.apache.thrift.async TAsyncClientManager TAsyncClientManager

List of usage examples for org.apache.thrift.async TAsyncClientManager TAsyncClientManager

Introduction

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

Prototype

public TAsyncClientManager() throws IOException 

Source Link

Usage

From source file:ParallelClient.java

License:Apache License

public static void main(String[] args) {

    if (args.length != 1 || !args[0].contains("simple")) {
        System.out.println("Please enter 'simple' ");
        System.exit(0);/*from   ww w. ja  v a2 s  .  com*/
    }

    try {

        for (int i = 0; i < 5; ++i) {
            System.out.println("Send request i = " + i);
            new Thread() {
                public void run() {
                    try {
                        TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
                        TAsyncClientManager clientManager = new TAsyncClientManager();
                        TNonblockingTransport transport = new TNonblockingSocket("localhost", 9090);
                        Myservice.AsyncClient client = new Myservice.AsyncClient(protocolFactory, clientManager,
                                transport);
                        client.DelayAdd(100, 200, 3, new AddCallBack(latch, transport));
                    } catch (TException x) {
                        x.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            System.out.println("After Send request i = " + i);
        }
        boolean wait = latch.await(30, TimeUnit.SECONDS);
        System.out.println("latch.await =:" + wait);

        System.out.println("Exiting client.");

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:ch.epfl.eagle.daemon.util.ThriftClientPool.java

License:Apache License

public ThriftClientPool(MakerFactory<T> maker) {
    pool = new GenericKeyedObjectPool<InetSocketAddress, T>(new PoolFactory(maker), getPoolConfig());
    try {/*from   w  w  w.  j a va2 s. c o m*/
        clientManager = new TAsyncClientManager();
    } catch (IOException e) {
        LOG.fatal(e);
    }
}

From source file:com.kurento.kmf.thrift.pool.MediaServerAsyncClientFactory.java

License:Open Source License

private AsyncClient createAsyncClient() {
    TNonblockingTransport transport;//from  w ww  .ja v  a 2 s .  c  om

    try {
        transport = new TNonblockingSocket(apiConfig.getServerAddress(), apiConfig.getServerPort());
    } catch (IOException e) {
        throw new KurentoMediaFrameworkException("Error creating non blocking transport", e, 30000);
    }

    TAsyncClientManager clientManager;
    try {
        clientManager = new TAsyncClientManager();
    } catch (IOException e) {
        throw new KurentoMediaFrameworkException("Error creating client manager", e, 30000);
    }

    TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();

    return new AsyncClientWithValidation(protocolFactory, clientManager, transport);
}

From source file:com.kurento.kmf.thrift.pool.ThriftAsyncClientFactory.java

License:Open Source License

/**
 * Default constructor, to be used in spring environments
 *///from   w ww.j  a  va 2 s.  co m
public ThriftAsyncClientFactory() {

    try {
        clientManager = new TAsyncClientManager();
    } catch (IOException e) {
        throw new ClientPoolException("Error creating client manager", e);
    }
}

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

License:Apache License

public AsyncClientPool(int maxConnectionsPerHost, int connectionTimeout) throws IOException {
    _clientManager = new TAsyncClientManager();
    _protocolFactory = new TBinaryProtocol.Factory();
    _maxConnectionsPerHost = maxConnectionsPerHost;
    _timeout = connectionTimeout;/*from   www.j  a v  a  2  s .c o m*/
    try {
        _transportField = TAsyncClient.class.getDeclaredField("___transport");
        _transportField.setAccessible(true);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.rapleaf.hank.client.async.HankAsyncSmartClient.java

License:Apache License

/**
 * Create a new HankAsyncSmartClient that uses the supplied coordinator and works
 * with the requested ring group. Note that a given HankSmartClient can only
 * contact one ring group. Queries will timeout after the given period of time.
 * A timeout of 0 means no timeout./*from  w  w  w.  jav  a  2s  .c o m*/
 *
 * @param coordinator
 * @param ringGroupName
 * @param numConnectionsPerHost
 * @param queryTimeoutMs
 * @throws IOException
 * @throws TException
 */
public HankAsyncSmartClient(Coordinator coordinator, String ringGroupName, int numConnectionsPerHost,
        int queryMaxNumTries, int establishConnectionTimeoutMs, int queryTimeoutMs, int bulkQueryTimeoutMs)
        throws IOException, TException {
    this.coordinator = coordinator;
    ringGroup = coordinator.getRingGroup(ringGroupName);

    if (ringGroup == null) {
        throw new IOException(
                "Could not find Ring Group " + ringGroupName + " with Coordinator " + coordinator.toString());
    }

    this.numConnectionsPerHost = numConnectionsPerHost;
    this.establishConnectionTimeoutMs = establishConnectionTimeoutMs;
    this.queryTimeoutMs = queryTimeoutMs;
    this.bulkQueryTimeoutMs = bulkQueryTimeoutMs;

    // Start Dispatcher thread
    dispatcher = new Dispatcher(queryTimeoutMs, bulkQueryTimeoutMs, queryMaxNumTries);
    dispatcherThread = new DispatcherThread(dispatcher);
    dispatcherThread.start();

    // Start Connector thread
    connector = new Connector();
    connectorThread = new ConnectorThread(connector);
    connector.setConnectorThread(connectorThread);
    connectorThread.start();

    // Initialize asynchronous client manager
    int asyncClientManagerCount = 2;
    asyncClientManager = new ArrayList<TAsyncClientManager>();
    for (int i = 0; i < asyncClientManagerCount; ++i) {
        asyncClientManager.add(new TAsyncClientManager());
    }

    // Load cache
    loadCache(numConnectionsPerHost);
    ringGroup.addDataLocationChangeListener(this);
}

From source file:com.rapleaf.hank.client.async.TestHostConnection.java

License:Apache License

public void testQueryOnlyServingHosts() throws Exception, TException, InterruptedException {

    int establishConnectionTimeoutMs = 0;
    int queryTimeoutMs = 100;
    int bulkQueryTimeoutMs = 100;

    HostConnection connection = new HostConnection(mockHost, null, new TAsyncClientManager(),
            establishConnectionTimeoutMs, queryTimeoutMs, bulkQueryTimeoutMs);

    // Should not query a non serving host
    mockHost.setState(HostState.IDLE);//  w  w  w. j  av  a  2s. com
    connect(connection);
    try {
        asyncGet(connection, 0, KEY_1);
        fail("Should fail");
    } catch (HostConnection.IllegalStateException e) {
        // If we don't catch the exception then the test will fail
    }
    try {
        asyncGetBulk(connection, 0, Collections.singletonList(KEY_1));
        fail("Should fail");
    } catch (HostConnection.IllegalStateException e) {
        // If we don't catch the exception then the test will fail
    }

    // Should succeed quering a serving host
    mockHost.setState(HostState.SERVING);
    startMockPartitionServerThread(mockIface, 1);
    connect(connection);
    asyncGet(connection, 0, KEY_1);
    asyncGetBulk(connection, 0, Collections.singletonList(KEY_1));
}

From source file:com.rapleaf.hank.client.async.TestHostConnection.java

License:Apache License

public void testConnectionTimeout() throws IOException, TException {
    int establishConnectionTimeoutMs = 100;
    int queryTimeoutMs = 100;
    int bulkQueryTimeoutMs = 100;

    try {//from  w ww .j av  a 2s .c  o  m
        new HostConnection(mockHost, null, new TAsyncClientManager(), establishConnectionTimeoutMs,
                queryTimeoutMs, bulkQueryTimeoutMs);

        fail("Should fail");
    } catch (NotImplementedException e) {
        // If we don't catch the exception then the test will fail
    }
}

From source file:com.rapleaf.hank.client.async.TestHostConnection.java

License:Apache License

public void testListenerWakeup() throws IOException, TException {
    int queryTimeoutMs = 10;
    int bulkQueryTimeoutMs = 10;
    NotifierMock notifier = new NotifierMock();

    HostConnection connection = new HostConnection(mockHost, notifier, new TAsyncClientManager(), 0,
            queryTimeoutMs, bulkQueryTimeoutMs);
    mockHost.setState(HostState.SERVING);
    connect(connection);//  w w w  . j a va  2s  .  c  o  m

    assertTrue("Notifier should have been notified.", notifier.notified);
}

From source file:com.rapleaf.hank.client.async.TestHostConnection.java

License:Apache License

public void testGetTimeouts() throws IOException, TException, InterruptedException {

    mockHost.setState(HostState.SERVING);

    IfaceWithShutdown hangingIface = new IfaceWithShutdown() {
        @Override//from   w  w w. j a  v a2  s. c o  m
        public void shutDown() throws InterruptedException {
        }

        @Override
        public HankResponse get(int domain_id, ByteBuffer key) throws TException {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return null;
        }

        @Override
        public HankBulkResponse getBulk(int domain_id, List<ByteBuffer> keys) throws TException {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return null;
        }
    };

    // Start server
    startMockPartitionServerThread(hangingIface, 1);

    long duration = Long.MAX_VALUE;
    HankTimer timer = new HankTimer();

    HostConnection connection = new HostConnection(mockHost, null, new TAsyncClientManager(), 0, 100, 100);
    mockHost.setState(HostState.SERVING);

    // Test GET Timeout
    connect(connection);
    timer.restart();
    try {
        asyncGet(connection, 0, KEY_1);
        fail("Should fail");
    } catch (Exception e) {
        duration = timer.getDuration() / 1000000l;
        // Check that correct exception was raised
        assertTrue(e instanceof TimeoutException);
    }
    // Check that timeout was respected
    LOG.info("Took " + duration + "ms");
    // 10ms get timeout and 10ms for the rest
    assertTrue(duration < 100 + 10);

    // Test GET BULK Timeout
    connection.attemptDisconnect();
    connect(connection);
    timer.restart();
    try {
        asyncGetBulk(connection, 0, Collections.singletonList(KEY_1));
        fail("Should fail");
    } catch (Exception e) {
        duration = timer.getDuration() / 1000000l;
        // Check that correct exception was raised
        assertTrue(e instanceof TimeoutException);
    }
    // Check that timeout was respected
    LOG.info("Took " + duration + "ms");
    // 10ms get timeout and 10ms for the rest
    assertTrue(duration < 100 + 10);

    connection.attemptDisconnect();
}