Example usage for org.apache.thrift.transport TFramedTransport close

List of usage examples for org.apache.thrift.transport TFramedTransport close

Introduction

In this page you can find the example usage for org.apache.thrift.transport TFramedTransport close.

Prototype

public void close() 

Source Link

Usage

From source file:com.ning.metrics.collector.TestPerformance.java

License:Apache License

public static void main(String[] args) throws Exception {
    final TFramedTransport transport = createScribeClient();

    String message = createThriftPayload();
    //String message = createSmilePayload();

    NUMBER_OF_MESSAGES_PER_SCRIBE_CLIENT = 2000;
    doOneRun(message);/*from  w w w . j  av  a  2s. c om*/

    //NUMBER_OF_MESSAGES_PER_SCRIBE_CLIENT = 2500;
    //doOneRun(message);

    //NUMBER_OF_MESSAGES_PER_SCRIBE_CLIENT = 3000;
    //doOneRun(message);

    //NUMBER_OF_MESSAGES_PER_SCRIBE_CLIENT = 20000;
    //doOneRun(message);

    transport.close();

    System.exit(0);
}

From source file:org.apache.cassandra.http.HTTPCassandra.java

License:Apache License

public String[] handleGET(String[] query) {
    String keyspace = query[1];// ww w .ja v  a2  s  .co m
    String colFam = query[2];
    String key = query[3];
    String superColumn = query[4];
    String startColumn = query[5];
    String endColumn = query[6];

    String[] consistencyLevelReturnType = StringUtils.split(query[7], ".");
    String type = consistencyLevelReturnType[1];
    ConsistencyLevel CL = ConsistencyLevel.valueOf(consistencyLevelReturnType[0]);

    TTransport tr = new TSocket(HOST, PORT);
    TFramedTransport tf = new TFramedTransport(tr);
    TProtocol proto = new TBinaryProtocol(tf);
    Cassandra.Client client = new Cassandra.Client(proto);
    String retData = "";

    try {
        tf.open();
        client.set_keyspace(keyspace);
        byte[] userIDKey = key.getBytes();
        ColumnParent parent = new ColumnParent(colFam);

        SlicePredicate predicate = new SlicePredicate();
        SliceRange sliceRange = new SliceRange();
        sliceRange.setStart(startColumn.getBytes(UTF8));
        sliceRange.setFinish(endColumn.getBytes(UTF8));
        predicate.setSlice_range(sliceRange);
        List<ColumnOrSuperColumn> results = client.get_slice(ByteBuffer.wrap(userIDKey), parent, predicate, CL);

        //create return data
        if ((type.equalsIgnoreCase("json"))) {

            retData = toJSON(results, key);
            type = "application/json";
        } else if ((type.equalsIgnoreCase("xml"))) {

            retData = toXML(results, key);
            type = "text/xml";
        } else if ((type.equalsIgnoreCase("plain"))) {

            CharBuffer cbuf;
            for (ColumnOrSuperColumn result : results) {
                Column column = result.column;
                Charset charset = Charset.forName("UTF8");
                CharsetDecoder decoder = charset.newDecoder();
                cbuf = decoder.decode(column.name);
                retData += "Key: " + cbuf.toString() + " ";
                cbuf = decoder.decode(column.value);
                retData += "Value: " + cbuf.toString() + " \n";
            }
            type = "text/plain";
        }

        tf.close();
    } catch (Exception e) {
        String[] eRet = new String[2];
        if (e instanceof InvalidRequestException) {
            //keyspace or col fam might not exist
            eRet[0] = "400";
            eRet[1] = "Client Request error"; //todo: query inspection
        } else if (e instanceof UnavailableException) {
            eRet[0] = "503";
            eRet[1] = "Service Unavailable";
        } else if (e instanceof TimedOutException) {
            //not the best fit as 4xx codes are client errors, but this isnt a strict http implementation
            eRet[0] = "408";
            eRet[1] = "Request Timeout - Valid query try again later";
        } else {
            eRet[0] = "500";
            eRet[1] = "Internal Server Error";
        }
        return eRet;
    }
    //send return headers
    if (retData.equals("")) {
        String[] eRet = new String[2];
        eRet[0] = "404";
        eRet[1] = "Row Not Found";
        return eRet;
    } else {
        String[] wholeRowRet = new String[3];
        wholeRowRet[0] = "200";
        wholeRowRet[1] = retData;
        wholeRowRet[2] = type;
        return wholeRowRet;
    }
}

From source file:org.apache.cassandra.http.HTTPCassandra.java

License:Apache License

public String[] handlePOST(String[] query, String[] vars) {

    try {// w w  w  .  j a va  2s  .c o  m
        TTransport tr = new TSocket(HOST, PORT);
        TFramedTransport tf = new TFramedTransport(tr);
        TProtocol proto = new TBinaryProtocol(tf);
        Cassandra.Client client = new Cassandra.Client(proto);
        tf.open();

        client.set_keyspace(query[1]);
        ColumnParent cp = new ColumnParent(query[2]);
        byte[] userIDKey = query[3].getBytes();
        ConsistencyLevel CL = ConsistencyLevel.valueOf(query[7]);

        Long clock;

        for (String var : vars) {
            var = java.net.URLDecoder.decode(var, "ISO-8859-1");
            String[] keyval = var.split("=");
            clock = now.getTimeInMillis();
            client.insert(ByteBuffer.wrap(userIDKey), cp, new Column(ByteBuffer.wrap(keyval[0].getBytes(UTF8)),
                    ByteBuffer.wrap(keyval[1].getBytes()), clock), CL);
        }
        System.out.println("");
        tf.close();
    } catch (Exception e) {
        String[] eRet = new String[2];
        if (e instanceof InvalidRequestException) {
            //keyspace or col fam might not exist
            //see wiki discussion
            eRet[0] = "400";
            eRet[1] = "Client Request error"; //todo: query inspection
        } else if (e instanceof UnavailableException) {
            eRet[0] = "503";
            eRet[1] = "Service Unavailable";
        } else if (e instanceof TimedOutException) {
            //not the best fit as 4xx codes are client errors, but this isnt a strict http implementation
            eRet[0] = "408";
            eRet[1] = "Request Timeout - Valid query try again later";
        } else {
            eRet[0] = "500";
            eRet[1] = "Internal Server Error";
        }
        return eRet;
    }

    String[] ret = new String[2];
    ret[0] = "201";
    ret[1] = "Created";
    return ret;
}

From source file:org.apache.cassandra.http.HTTPCassandra.java

License:Apache License

public String[] handleDELETE(String[] query) {
    String keyspace = query[1];//  w  w  w  .j a  v  a  2 s. c  o  m
    String colFam = query[2];
    String key = query[3];
    String superColumn = query[4];
    String startColumn = query[5];
    String endColumn = query[6];
    ConsistencyLevel CL = ConsistencyLevel.valueOf(query[7]);

    TTransport tr = new TSocket(HOST, PORT);
    TFramedTransport tf = new TFramedTransport(tr);
    TProtocol proto = new TBinaryProtocol(tf);
    Cassandra.Client client = new Cassandra.Client(proto);
    String retData = "";
    long timestamp = 0;

    try {
        tf.open();
        client.set_keyspace(keyspace);
        byte[] userIDKey = key.getBytes();
        ColumnParent parent = new ColumnParent(colFam);

        SlicePredicate predicate = new SlicePredicate();
        SliceRange sliceRange = new SliceRange();
        sliceRange.setStart(startColumn.getBytes(UTF8));
        sliceRange.setFinish(endColumn.getBytes(UTF8));
        predicate.setSlice_range(sliceRange);
        List<ColumnOrSuperColumn> results = client.get_slice(ByteBuffer.wrap(userIDKey), parent, predicate, CL);
        //doesnt need to loop over all
        for (ColumnOrSuperColumn result : results) {
            timestamp = result.column.getTimestamp();
        }

        ColumnPath colPath = new ColumnPath(colFam);
        client.remove(ByteBuffer.wrap(userIDKey), colPath, timestamp, ConsistencyLevel.ONE);
        System.out.println("del");
        tf.close();
    } catch (Exception e) {
        String[] eRet = new String[2];
        if (e instanceof InvalidRequestException) {
            //keyspace or col fam might not exist
            eRet[0] = "400";
            eRet[1] = "Client Request error"; //todo: query inspection
        } else if (e instanceof UnavailableException) {
            eRet[0] = "503";
            eRet[1] = "Service Unavailable";
        } else if (e instanceof TimedOutException) {
            //not the best fit as 4xx codes are client errors, but this isnt a strict http implementation
            eRet[0] = "408";
            eRet[1] = "Request Timeout - Valid query try again later";
        } else {
            eRet[0] = "500";
            eRet[1] = "Internal Server Error";
        }
        return eRet;
    }
    String[] ret = new String[2];
    ret[0] = "201";
    ret[1] = "Created";
    return ret;

}

From source file:org.jboss.jgroups.cassandra.plugins.BaseCassandraSPI.java

License:Open Source License

protected <T> T execute(ClientExecutor<T> executor) {
    TTransport tr = new TSocket(host, port);
    TFramedTransport tf = new TFramedTransport(tr);
    TProtocol proto = new TBinaryProtocol(tf);
    Cassandra.Client client = new Cassandra.Client(proto);
    try {/*  ww w. ja v a 2  s  .  co  m*/
        tf.open();
        return executor.execute(client);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    } finally {
        tf.close();
    }
}

From source file:SendFile.StreamFileClient.java

public static void main(String[] args) throws IOException {
    try {/*from  ww w.j ava 2 s . co m*/
        TFramedTransport transport;
        TTransport socket = new TSocket("localhost", 9090);
        transport = new TFramedTransport(socket);
        try {
            transport.open();
            // Set socket timeout to regular mode
        } catch (TTransportException e) {
            System.out.println("Failed to establish connection to host " + e.toString());
            transport.close();
            throw new IOException("Failed to establish connection to host ", e);
        }
        TProtocol protocol = new TBinaryProtocol(transport);
        StreamFileService.Client client = new StreamFileService.Client(protocol);
        perform(client);
        transport.close();
    } catch (TException x) {
        x.printStackTrace();
    }
}