Search in sources :

Example 51 with TCompactProtocol

use of org.apache.thrift.protocol.TCompactProtocol in project tech by ffyyhh995511.

the class ThriftService2 method getDistributedIncrease.

/**
 * 分布式递增
 * @return
 */
public long getDistributedIncrease() {
    int timeout = 100 * 1000;
    TTransport transport = null;
    Long increase = 0L;
    try {
        // 设置传输通道,对于非阻塞服务,需要使用TFramedTransport,它将数据分块发送
        transport = new TFramedTransport(new TSocket("192.168.11.173", 7911, timeout));
        transport.open();
        // 使用高密度二进制协议
        TProtocol protocol = new TCompactProtocol(transport);
        // 创建Client
        ServerTime.Client client = new ServerTime.Client(protocol);
        increase = client.getIncrease();
    } catch (Exception e) {
        // logger.error(e.getStackTrace());
        e.printStackTrace();
    } finally {
        // 关闭资源
        if (transport != null) {
            transport.close();
        }
    }
    return increase;
}
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) TSocket(org.apache.thrift.transport.TSocket)

Example 52 with TCompactProtocol

use of org.apache.thrift.protocol.TCompactProtocol 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 53 with TCompactProtocol

use of org.apache.thrift.protocol.TCompactProtocol 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 54 with TCompactProtocol

use of org.apache.thrift.protocol.TCompactProtocol in project tech by ffyyhh995511.

the class Test1Client method main.

public static void main(String[] args) throws Exception {
    // 设置传输通道 - 普通IO流通道
    TTransport transport = new TSocket("localhost", 7911);
    transport.open();
    // 使用高密度二进制协议
    TProtocol protocol = new TCompactProtocol(transport);
    // 创建Client
    Hello.Client client = new Hello.Client(protocol);
    long start = System.currentTimeMillis();
    for (int i = 0; i < 10000; i++) {
        client.helloBoolean(false);
        client.helloInt(111);
        // client.helloNull();
        client.helloString("dongjian");
        client.helloVoid();
    }
    System.out.println("耗时:" + (System.currentTimeMillis() - start));
    // 关闭资源
    transport.close();
}
Also used : Hello(org.tech.model.Hello) TProtocol(org.apache.thrift.protocol.TProtocol) TTransport(org.apache.thrift.transport.TTransport) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TSocket(org.apache.thrift.transport.TSocket)

Aggregations

TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)54 TProtocol (org.apache.thrift.protocol.TProtocol)38 THttpClient (org.apache.thrift.transport.THttpClient)18 TSocket (org.apache.thrift.transport.TSocket)18 TException (org.apache.thrift.TException)15 TTransport (org.apache.thrift.transport.TTransport)11 TFramedTransport (org.apache.thrift.transport.TFramedTransport)9 IOException (java.io.IOException)8 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)7 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)7 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)7 TTransportException (org.apache.thrift.transport.TTransportException)5 TFramedTransport (org.apache.thrift.transport.layered.TFramedTransport)5 Test (org.junit.Test)5 LoginException (javax.security.auth.login.LoginException)4 Hello (org.tech.model.Hello)4 URI (java.net.URI)3 ArrayList (java.util.ArrayList)3 UGIAssumingTransport (org.apache.accumulo.core.rpc.UGIAssumingTransport)3 AccumuloProxy (org.apache.accumulo.proxy.thrift.AccumuloProxy)3