Example usage for org.apache.thrift.transport TSocket isOpen

List of usage examples for org.apache.thrift.transport TSocket isOpen

Introduction

In this page you can find the example usage for org.apache.thrift.transport TSocket isOpen.

Prototype

public boolean isOpen() 

Source Link

Document

Checks whether the socket is connected.

Usage

From source file:com.impetus.client.cassandra.schemamanager.CassandraSchemaManager.java

License:Apache License

/**
 * initiate client method initiates the client.
 * /*from   w  w  w  .  java2s.  com*/
 * @return boolean value ie client started or not.
 * 
 */
protected boolean initiateClient() {
    Throwable message = null;

    for (String host : hosts) {
        if (host == null || !StringUtils.isNumeric(port) || port.isEmpty()) {
            log.error("Host or port should not be null, Port should be numeric.");
            throw new IllegalArgumentException("Host or port should not be null, Port should be numeric.");
        }
        int thriftPort = externalProperties.get(CassandraConstants.THRIFT_PORT) != null
                ? Integer.parseInt((String) externalProperties.get(CassandraConstants.THRIFT_PORT))
                : Integer.parseInt(port);
        TSocket socket = new TSocket(host, thriftPort);
        TTransport transport = new TFramedTransport(socket);
        TProtocol protocol = new TBinaryProtocol(transport, true, true);
        cassandra_client = new Cassandra.Client(protocol);
        try {
            if (!socket.isOpen()) {
                socket.open();
                if (userName != null) {
                    Map<String, String> credentials = new HashMap<String, String>();
                    credentials.put("username", userName);
                    credentials.put("password", password);
                    AuthenticationRequest auth_request = new AuthenticationRequest(credentials);
                    cassandra_client.login(auth_request);
                }
            }
            return true;
        } catch (TTransportException e) {
            message = e;
            log.warn("Error while opening socket for host {}, skipping for next available node ", host);
        } catch (Exception e) {
            log.error("Error during creating schema in cassandra, Caused by: .", e);
            throw new SchemaGenerationException(e, "Cassandra");
        }
    }
    throw new SchemaGenerationException("Error while opening socket, Caused by: .", message, "Cassandra");
}

From source file:com.impetus.kundera.ycsb.utils.CassandraOperationUtils.java

License:Apache License

private void initiateClient(String host, Runtime runtime, int port) throws InterruptedException, IOException {
    while (checkOnProcess(runtime)) {
        TSocket socket = new TSocket(host, port);
        TTransport transport = new TFramedTransport(socket);
        TProtocol protocol = new TBinaryProtocol(transport, true, true);
        cassandra_client = new Cassandra.Client(protocol);
        try {//  ww  w.  ja  va  2 s.c o  m
            if (!socket.isOpen()) {
                socket.open();
            }
        } catch (TTransportException e) {
            logger.error(e);
        } catch (NumberFormatException e) {
            logger.error(e);
        }
        TimeUnit.SECONDS.sleep(3);
        return;
    }
}

From source file:io.airlift.drift.transport.apache.ApacheThriftMethodInvoker.java

License:Apache License

private Object invokeSynchronous(InvokeRequest request, ResultHandler resultHandler) throws Exception {
    List<HostAndPort> addresses = addressSelector.getAddresses(request.getAddressSelectionContext());
    if (addresses.isEmpty()) {
        throw new TTransportException("No hosts available");
    }//from   w w  w . ja  va2  s. co  m

    Exception lastException = null;
    for (HostAndPort address : addresses) {
        TSocket socket = createTSocket(address);
        if (!socket.isOpen()) {
            try {
                socket.open();
            } catch (org.apache.thrift.transport.TTransportException e) {
                addressSelector.markdown(address);
                continue;
            }
        }

        try {
            TTransport transport = transportFactory.getTransport(socket);
            TProtocol protocol = protocolFactory.getProtocol(transport);

            writeRequest(request.getMethod(), request.getParameters(), protocol);

            return readResponse(request.getMethod(), protocol);
        } catch (Exception e) {
            if (resultHandler.isHostDownException(e)) {
                addressSelector.markdown(address);
            }
            if (!resultHandler.isRetryable(e)) {
                throw e;
            }
            lastException = e;
        } finally {
            socket.close();
        }
    }
    if (lastException == null) {
        throw new TTransportException("Unable to connect to any hosts");
    }
    throw lastException;
}

From source file:io.airlift.drift.transport.apache.client.ApacheThriftMethodInvoker.java

License:Apache License

private Object invokeSynchronous(InvokeRequest request) throws Exception {
    Address address = request.getAddress();

    TSocket socket = createTSocket(address.getHostAndPort());
    if (!socket.isOpen()) {
        try {//from   w w w .  j  a  va2 s . c  o m
            socket.open();
        } catch (org.apache.thrift.transport.TTransportException e) {
            throw new ConnectionFailedException(address, e);
        }
    }

    try {
        TTransport transport = transportFactory.getTransport(socket);
        TProtocol protocol = protocolFactory.getProtocol(transport);

        writeRequest(request.getMethod(), request.getParameters(), protocol);

        return readResponse(request.getMethod(), protocol);
    } finally {
        socket.close();
    }
}