Example usage for org.apache.thrift.protocol TBinaryProtocol TBinaryProtocol

List of usage examples for org.apache.thrift.protocol TBinaryProtocol TBinaryProtocol

Introduction

In this page you can find the example usage for org.apache.thrift.protocol TBinaryProtocol TBinaryProtocol.

Prototype

public TBinaryProtocol(TTransport trans) 

Source Link

Document

Constructor

Usage

From source file:appSetup.java

License:Apache License

private static Cassandra.Client createConnection(String host, Integer port, boolean framed)
        throws TTransportException {
    TSocket socket = new TSocket(host, port);
    TTransport trans = framed ? new TFramedTransport(socket) : socket;
    trans.open();/*from w  w  w  .j a v a2 s  . com*/
    TProtocol protocol = new TBinaryProtocol(trans);

    return new Cassandra.Client(protocol);
}

From source file:JavaClient2.java

License:Apache License

public static void main(String[] args) {

    // 1st args is "simple", 2nd args is server address
    if (args.length != 2 || !args[0].contains("simple")) {
        System.out.println("Please enter 'simple' ");
        System.exit(0);/*from   w w w .  j a va  2s .  c om*/
    }

    try {
        TTransport transport;
        transport = new TSocket(args[1], 14264);
        transport.open();

        TProtocol protocol = new TBinaryProtocol(transport);
        Myservice.Client client = new Myservice.Client(protocol);

        perform(client);

        transport.close();
    } catch (TException x) {
        x.printStackTrace();
    }
}

From source file:SClient.java

License:Apache License

public SClient(String s, int p) {
    this.ip = s;/*from  w  w w.j a va 2s .c o m*/
    this.port = p;
    if (transport == null)
        transport = new TSocket(this.ip, this.port);
    if (protocol == null) {
        protocol = new TBinaryProtocol(transport);
        client = new Sentiments.Client(protocol);
        try {
            transport.open();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }

}

From source file:FilesystemClient.java

public static void main(String[] args) {
    try {/*from   ww  w .  ja va 2  s  . c o m*/
        TTransport transport;
        transport = new TSocket("localhost", 9090);
        transport.open();
        TProtocol protocol = new TBinaryProtocol(transport);
        FilesystemService.Client client = new FilesystemService.Client(protocol);
        perform(client);
        transport.close();
    } catch (TException x) {
        x.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:JavaClient.java

License:Apache License

public static void main(String[] args) {

    if (args.length != 1) {
        System.out.println("Please enter 'simple' or 'secure'");
        System.exit(0);//from   w ww . ja  v a 2  s. c om
    }

    try {
        TTransport transport;
        if (args[0].contains("simple")) {
            transport = new TSocket("localhost", 9090);
            transport.open();
        } else {
            /*
             * Similar to the server, you can use the parameters to setup client parameters or
             * use the default settings. On the client side, you will need a TrustStore which
             * contains the trusted certificate along with the public key. 
             * For this example it's a self-signed cert. 
             */
            TSSLTransportParameters params = new TSSLTransportParameters();
            params.setTrustStore("../../lib/java/test/.truststore", "thrift", "SunX509", "JKS");
            /*
             * Get a client transport instead of a server transport. The connection is opened on
             * invocation of the factory method, no need to specifically call open()
             */
            transport = TSSLTransportFactory.getClientSocket("localhost", 9091, 0, params);
        }

        TProtocol protocol = new TBinaryProtocol(transport);
        Calculator.Client client = new Calculator.Client(protocol);

        perform(client);

        transport.close();
    } catch (TException x) {
        x.printStackTrace();
    }
}

From source file:StreamerClientTest.java

License:Open Source License

public static void main(String[] args) throws Exception {

    TTransport transport;//from  w w w . j a v a 2 s .  co m

    transport = new TSocket("localhost", 7911);

    TProtocol protocol = new TBinaryProtocol(transport);

    Streamer.Client client = new Streamer.Client(protocol);
    transport.open();

    String accessKey = "defaultAccessKey";
    String secretKey = "defaultSecretKey";

    int count = 100;
    Random rnd = new Random();
    for (int i = 0; i < count; i++) {
        Tuple tuple = new Tuple();

        String id = UUID.randomUUID().toString();
        tuple.setId(id);

        String topic = "testTopic01";
        tuple.setTopic(topic);

        String correlationId = UUID.randomUUID().toString();
        tuple.setCorrelationId(correlationId);

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("category", "test");
        tuple.setHeaders(headers);

        Map<String, String> payload = new HashMap<String, String>();
        payload.put("id", 6900 + i + "");
        String name = "Aditya" + rnd.nextInt();
        payload.put("name", name);
        tuple.setPayload(payload);

        client.tellTuple(accessKey, secretKey, tuple);
        System.out.println("Sent: " + name);
    }

    transport.close();
    System.out.println("Done!");
    System.exit(0);
}

From source file:MapDExample.java

License:Apache License

public static MapD.Client get_client(String host_or_uri, int port, boolean http) {
    THttpClient httpTransport;/*from   w  w  w  . j a v a2s .c  o m*/
    TTransport transport;
    TBinaryProtocol protocol;
    TJSONProtocol jsonProtocol;
    TSocket socket;
    MapD.Client client;

    try {
        if (http) {
            httpTransport = new THttpClient(host_or_uri);
            jsonProtocol = new TJSONProtocol(httpTransport);
            client = new MapD.Client(jsonProtocol);
            httpTransport.open();
            return client;
        } else {
            transport = new TSocket(host_or_uri, port);
            protocol = new TBinaryProtocol(transport);
            client = new MapD.Client(protocol);
            transport.open();
            return client;
        }
    } catch (TException x) {
        x.printStackTrace();
    }
    return null;
}

From source file:agiato.cassandra.connect.Connector.java

License:Apache License

public Cassandra.Client openConnection() {
    try {/*from w  w  w  .java  2 s  . c o  m*/
        if (null == trans) {
            //trans = new TSocket("localhost", 9160);
            trans = new TFramedTransport(new TSocket(getHost().getName(), getHost().getPort()));

            TProtocol proto = new TBinaryProtocol(trans);
            client = new Cassandra.Client(proto);
            trans.open();
        }
    } catch (TTransportException exception) {
        System.out.println("failed to open Cassandra connection" + exception);
        exception.printStackTrace();
    }

    return client;
}

From source file:alluxio.AbstractClient.java

License:Apache License

/**
 * Connects with the remote./*from   ww  w.  j av  a2 s  .  co  m*/
 */
public synchronized void connect() throws IOException {
    if (mConnected) {
        return;
    }
    disconnect();
    Preconditions.checkState(!mClosed, "Client is closed, will not try to connect.");

    RetryPolicy retryPolicy = new ExponentialBackoffRetry(BASE_SLEEP_MS, MAX_SLEEP_MS, RPC_MAX_NUM_RETRY);
    while (!mClosed) {
        mAddress = getAddress();
        LOG.info("Alluxio client (version {}) is trying to connect with {} @ {}", RuntimeConstants.VERSION,
                getServiceName(), mAddress);

        TProtocol binaryProtocol = new TBinaryProtocol(
                mTransportProvider.getClientTransport(mParentSubject, mAddress));
        mProtocol = new TMultiplexedProtocol(binaryProtocol, getServiceName());
        try {
            mProtocol.getTransport().open();
            LOG.info("Client registered with {} @ {}", getServiceName(), mAddress);
            mConnected = true;
            afterConnect();
            checkVersion(getClient(), getServiceVersion());
            return;
        } catch (IOException e) {
            if (e.getMessage() != null && FRAME_SIZE_EXCEPTION_PATTERN.matcher(e.getMessage()).find()) {
                // See an error like "Frame size (67108864) larger than max length (16777216)!",
                // pointing to the helper page.
                String message = String.format(
                        "Failed to connect with %s @ %s: %s. "
                                + "This exception may be caused by incorrect network configuration. "
                                + "Please consult %s for common solutions to address this problem.",
                        getServiceName(), mAddress, e.getMessage(), RuntimeConstants.ALLUXIO_DEBUG_DOCS_URL);
                throw new UnimplementedException(message, e);
            }
            throw e;
        } catch (TTransportException e) {
            LOG.warn("Failed to connect ({}) with {} @ {}: {}", retryPolicy.getRetryCount(), getServiceName(),
                    mAddress, e.getMessage());
            if (e.getCause() instanceof java.net.SocketTimeoutException) {
                // Do not retry if socket timeout.
                String message = "Thrift transport open times out. Please check whether the "
                        + "authentication types match between client and server. Note that NOSASL client "
                        + "is not able to connect to servers with SIMPLE security mode.";
                throw new UnavailableException(message, e);
            }
            // TODO(peis): Consider closing the connection here as well.
            if (!retryPolicy.attemptRetry()) {
                break;
            }
        }
    }
    // Reaching here indicates that we did not successfully connect.
    throw new UnavailableException(String.format("Failed to connect to %s @ %s after %s attempts",
            getServiceName(), mAddress, retryPolicy.getRetryCount()));
}

From source file:alluxio.client.block.BlockWorkerClient.java

License:Apache License

/**
 * Opens the connection to the worker. And start the heartbeat thread.
 *
 * @throws IOException if a non-Alluxio exception occurs
 *//*from   w  ww . j  a v a 2s  .c  o  m*/
private synchronized void connectOperation() throws IOException {
    if (!mConnected) {
        LOG.info("Connecting to {} worker @ {}", (mIsLocal ? "local" : "remote"), mAddress);

        TProtocol binaryProtocol = new TBinaryProtocol(mTransportProvider.getClientTransport(mAddress));
        mProtocol = new TMultiplexedProtocol(binaryProtocol, getServiceName());
        mClient = new BlockWorkerClientService.Client(mProtocol);

        try {
            mProtocol.getTransport().open();
        } catch (TTransportException e) {
            LOG.error(e.getMessage(), e);
            return;
        }
        mConnected = true;

        // only start the heartbeat thread if the connection is successful and if there is not
        // another heartbeat thread running
        if (mHeartbeat == null || mHeartbeat.isCancelled() || mHeartbeat.isDone()) {
            final int interval = Configuration.getInt(Constants.USER_HEARTBEAT_INTERVAL_MS);
            mHeartbeat = mExecutorService
                    .submit(new HeartbeatThread(HeartbeatContext.WORKER_CLIENT, mHeartbeatExecutor, interval));
        }
    }
}