Search in sources :

Example 21 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport 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 22 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport 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 23 with TFramedTransport

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

the class Test3 method main.

public static void main(String[] args2) {
    try {
        // 非阻塞式的,配合TFramedTransport使用
        TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
        // 关联处理器与Service服务的实现
        TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloImpl());
        // 目前Thrift提供的最高级的模式,可并发处理客户端请求
        TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(serverTransport);
        args.processor(processor);
        // 设置协议工厂,高效率的、密集的二进制编码格式进行数据传输协议
        args.protocolFactory(new TCompactProtocol.Factory());
        // 设置传输工厂,使用非阻塞方式,按块的大小进行传输,类似于Java中的NIO
        args.transportFactory(new TFramedTransport.Factory());
        // 设置处理器工厂,只返回一个单例实例
        args.processorFactory(new TProcessorFactory(processor));
        // 多个线程,主要负责客户端的IO处理
        args.selectorThreads(2);
        // 工作线程池
        ExecutorService pool = Executors.newFixedThreadPool(3);
        args.executorService(pool);
        TThreadedSelectorServer server = new TThreadedSelectorServer(args);
        System.out.println("Starting server on port " + 7911 + "......");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) TProcessor(org.apache.thrift.TProcessor) TThreadedSelectorServer(org.apache.thrift.server.TThreadedSelectorServer) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) TProcessorFactory(org.apache.thrift.TProcessorFactory) HelloImpl(org.tech.model.impl.HelloImpl) TProcessor(org.apache.thrift.TProcessor) Hello(org.tech.model.Hello) TFramedTransport(org.apache.thrift.transport.TFramedTransport) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) ExecutorService(java.util.concurrent.ExecutorService)

Example 24 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport 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 25 with TFramedTransport

use of org.apache.thrift.transport.TFramedTransport in project summer by foxsugar.

the class TransportManager method getTransport.

public static TTransport getTransport(String host, int port) throws TTransportException {
    TTransport transport = new TFramedTransport(new TSocket(host.trim(), port));
    transport.open();
    return transport;
}
Also used : TFramedTransport(org.apache.thrift.transport.TFramedTransport) TTransport(org.apache.thrift.transport.TTransport) TSocket(org.apache.thrift.transport.TSocket)

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