Search in sources :

Example 36 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project tech by ffyyhh995511.

the class RedisDistributedLock method serverTimeMillis.

/**
 * 获取服务器时间
 * @return
 */
private long serverTimeMillis() {
    long time = 0;
    TTransport transport = null;
    try {
        // 设置传输通道,对于非阻塞服务,需要使用TFramedTransport,它将数据分块发送
        transport = new TFramedTransport(new TSocket("192.168.11.173", 7911));
        transport.open();
        // 使用高密度二进制协议
        TProtocol protocol = new TCompactProtocol(transport);
        // 创建Client
        ServerTime.Client client = new ServerTime.Client(protocol);
        time = client.getTime();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (transport != null) {
            // 关闭资源
            transport.close();
        }
    }
    return time;
}
Also used : TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) ServerTime(org.tech.commons.model.ServerTime) IOException(java.io.IOException) TSocket(org.apache.thrift.transport.TSocket)

Example 37 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project tech by ffyyhh995511.

the class ThriftDemo method nioClient.

/**
 * 编写客户端,调用(阻塞式IO + 多线程处理)服务
 */
public void nioClient() {
    try {
        // 设置传输通道,对于非阻塞服务,需要使用TFramedTransport,它将数据分块发送
        TTransport transport = new TFramedTransport(new TSocket("localhost", 7911));
        transport.open();
        // 使用高密度二进制协议
        TProtocol protocol = new TCompactProtocol(transport);
        // 创建Client
        Hello.Client client = new Hello.Client(protocol);
        System.out.println("starting...");
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            client.helloBoolean(false);
            client.helloInt(111);
            // client.helloNull();
            client.helloString("360buy");
            client.helloVoid();
        }
        System.out.println("耗时:" + (System.currentTimeMillis() - start));
        // 关闭资源
        transport.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Hello(org.tech.model.Hello) TProtocol(org.apache.thrift.protocol.TProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TSocket(org.apache.thrift.transport.TSocket)

Example 38 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project brisk by riptano.

the class BriskTool method getConnection.

private Brisk.Iface getConnection() throws IOException {
    TTransport trans = new TFramedTransport(new TSocket(host, port));
    try {
        trans.open();
    } catch (TTransportException e) {
        throw new IOException("unable to connect to brisk server");
    }
    Brisk.Iface client = new Brisk.Client(new TBinaryProtocol(trans));
    return client;
}
Also used : TBinaryProtocol(org.apache.cassandra.thrift.TBinaryProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) Brisk(org.apache.cassandra.thrift.Brisk) Iface(org.apache.cassandra.thrift.Brisk.Iface) TTransportException(org.apache.thrift.transport.TTransportException) TTransport(org.apache.thrift.transport.TTransport) IOException(java.io.IOException) TSocket(org.apache.thrift.transport.TSocket)

Example 39 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project Honu by jboulon.

the class TSocketMessageSender method openConnection.

protected void openConnection() throws CommunicationException {
    try {
        if (socket != null) {
            try {
                socket.close();
            } catch (Exception ignored) {
            }
            socket = null;
        }
        if (transport != null) {
            try {
                transport.close();
            } catch (Exception ignored) {
            }
            transport = null;
        }
        if (protocol != null) {
            protocol = null;
        }
        if (collector != null) {
            collector = null;
        }
        currentCollectorInfo = CollectorRegistry.getInstance().getCollector();
        if (currentCollectorInfo == null) {
            throw new CommunicationException("collector is null");
        }
        socket = new TSocket(currentCollectorInfo.getHost(), currentCollectorInfo.getPort());
        socket.setTimeout(SENDER_TIMEOUT);
        transport = new TFramedTransport(socket);
        protocol = new TBinaryProtocol(transport);
        collector = new HonuCollector.Client(protocol);
        transport.open();
        int status = collector.getStatus();
        if (status != ServiceStatus.ALIVE) {
            throw new RuntimeException("Collector is not alive! -- status:" + status);
        }
    } catch (Throwable e) {
        collector = null;
        throw new CommunicationException(e);
    }
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) HonuCollector(org.honu.thrift.HonuCollector) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 40 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project jstorm by alibaba.

the class SimpleTransportPlugin method connect.

/**
 * Connect to the specified server via framed transport
 *
 * @param transport The underlying Thrift transport.
 * @param serverHost unused.
 * @param asUser unused.
 */
@Override
public TTransport connect(TTransport transport, String serverHost, String asUser) throws TTransportException {
    int maxBufferSize = type.getMaxBufferSize(storm_conf);
    // create a framed transport
    TTransport conn = new TFramedTransport(transport, maxBufferSize);
    // connect
    conn.open();
    LOG.debug("Simple client transport has been established");
    return conn;
}
Also used : TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport)

Aggregations

TFramedTransport (org.apache.thrift.transport.TFramedTransport)45 TSocket (org.apache.thrift.transport.TSocket)41 TTransport (org.apache.thrift.transport.TTransport)32 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)25 TProtocol (org.apache.thrift.protocol.TProtocol)24 TException (org.apache.thrift.TException)14 IOException (java.io.IOException)11 TTransportException (org.apache.thrift.transport.TTransportException)11 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)10 Cassandra (org.apache.cassandra.thrift.Cassandra)4 Hello (org.tech.model.Hello)4 SocketException (java.net.SocketException)3 HashMap (java.util.HashMap)3 GameRPC (com.code.server.rpc.idl.GameRPC)2 UnknownHostException (java.net.UnknownHostException)2 SSLSocket (javax.net.ssl.SSLSocket)2 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)2 ConfigurationException (org.apache.cassandra.config.ConfigurationException)2 AuthenticationRequest (org.apache.cassandra.thrift.AuthenticationRequest)2 TBinaryProtocol (org.apache.cassandra.thrift.TBinaryProtocol)2