Search in sources :

Example 41 with TTransport

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

the class Session method getClient.

/**
 * Thrift client connection
 * @param setKeyspace - should we set keyspace for client or not
 * @return cassandra client connection
 */
public Cassandra.Client getClient(boolean setKeyspace) {
    // random node selection for fake load balancing
    String currentNode = nodes[Pricer.randomizer.nextInt(nodes.length)];
    TSocket socket = new TSocket(currentNode, port);
    TTransport transport = (isUnframed()) ? socket : new TFramedTransport(socket);
    Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(transport));
    try {
        transport.open();
        if (setKeyspace) {
            client.set_keyspace("PortfolioDemo");
        }
    } catch (InvalidRequestException e) {
        throw new RuntimeException(e.getWhy());
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return client;
}
Also used : TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Example 42 with TTransport

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

the class Test2Client method main.

public static void main(String[] args) throws Exception {
    // 设置传输通道,对于非阻塞服务,需要使用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();
}
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 43 with TTransport

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

the class Test3Client method main.

public static void main(String[] args) throws Exception {
    String address = "localhost";
    int port = 7911;
    int timeout = 100 * 1000;
    // 使用非阻塞方式,按块的大小进行传输,类似于Java中的NIO。记得调用close释放资源
    TTransport transport = new TFramedTransport(new TSocket(address, port, timeout));
    // 高效率的、密集的二进制编码格式进行数据传输协议
    TProtocol protocol = new TCompactProtocol(transport);
    Hello.Client client = new Hello.Client(protocol);
    try {
        transport.open();
        System.out.println(client.helloInt(111));
        transport.close();
    } catch (TException e) {
        e.printStackTrace();
    }
}
Also used : TException(org.apache.thrift.TException) 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 44 with TTransport

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

the class ThriftService method getDistributedIncrease.

/**
 * 分布式递增
 * @return
 */
public long getDistributedIncrease() {
    TTransport transport = null;
    Long increase = 0L;
    try {
        // 设置传输通道,对于非阻塞服务,需要使用TFramedTransport,它将数据分块发送
        transport = new TFramedTransport(new TSocket("192.168.11.170", 7911));
        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) TException(org.apache.thrift.TException) TSocket(org.apache.thrift.transport.TSocket)

Example 45 with TTransport

use of org.apache.thrift.transport.TTransport in project metacat by Netflix.

the class CatalogThriftEventHandler method createContext.

/**
 * {@inheritDoc}
 */
@Override
public ServerContext createContext(final TProtocol input, final TProtocol output) {
    final String userName = "metacat-thrift-interface";
    // requestContext.getHeaderString("X-Forwarded-For");
    String clientHost = null;
    final long requestThreadId = Thread.currentThread().getId();
    final TTransport transport = input.getTransport();
    if (transport instanceof TSocket) {
        final TSocket thriftSocket = (TSocket) transport;
        clientHost = thriftSocket.getSocket().getInetAddress().getHostAddress();
    }
    final CatalogServerRequestContext context = new CatalogServerRequestContext(userName, null, clientHost, null, "hive", requestThreadId);
    MetacatContextManager.setContext(context);
    return context;
}
Also used : TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

Aggregations

TTransport (org.apache.thrift.transport.TTransport)99 TSocket (org.apache.thrift.transport.TSocket)43 TProtocol (org.apache.thrift.protocol.TProtocol)42 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)36 TFramedTransport (org.apache.thrift.transport.TFramedTransport)28 TTransportException (org.apache.thrift.transport.TTransportException)20 Test (org.junit.Test)20 TException (org.apache.thrift.TException)18 IOException (java.io.IOException)12 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)10 ArrayList (java.util.ArrayList)9 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)8 HashMap (java.util.HashMap)6 Socket (java.net.Socket)5 TCLIService (org.apache.hive.service.rpc.thrift.TCLIService)5 THttpClient (org.apache.thrift.transport.THttpClient)4 TSaslClientTransport (org.apache.thrift.transport.TSaslClientTransport)4 ChannelBuffer (com.alibaba.dubbo.remoting.buffer.ChannelBuffer)3 Request (com.alibaba.dubbo.remoting.exchange.Request)3 Demo (com.alibaba.dubbo.rpc.gen.thrift.Demo)3