Search in sources :

Example 31 with TServer

use of org.apache.thrift.server.TServer in project tech by ffyyhh995511.

the class Test1 method main.

/**
 * 编写服务端,发布(阻塞式IO + 多线程处理)服务
 *
 * @param args
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
    try {
        // 设置传输通道,普通通道
        TServerTransport serverTransport = new TServerSocket(7911);
        // 使用高密度二进制协议
        TProtocolFactory proFactory = new TCompactProtocol.Factory();
        // 设置处理器HelloImpl
        TProcessor processor = new Hello.Processor(new HelloImpl());
        // 创建服务器
        Args args2 = new Args(serverTransport);
        args2.protocolFactory(proFactory);
        args2.processor(processor);
        TServer server = new TThreadPoolServer(args2);
        System.out.println("Start server on port 7911...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TServerSocket(org.apache.thrift.transport.TServerSocket) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) Args(org.apache.thrift.server.TThreadPoolServer.Args) TProcessor(org.apache.thrift.TProcessor) HelloImpl(org.tech.model.impl.HelloImpl) TProcessor(org.apache.thrift.TProcessor) TServer(org.apache.thrift.server.TServer) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TThreadPoolServer(org.apache.thrift.server.TThreadPoolServer) TServerTransport(org.apache.thrift.transport.TServerTransport)

Example 32 with TServer

use of org.apache.thrift.server.TServer in project tech by ffyyhh995511.

the class Test2 method main.

public static void main(String[] args) {
    try {
        // 传输通道 - 非阻塞方式
        TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
        // 异步IO,需要使用TFramedTransport,它将分块缓存读取。
        TTransportFactory transportFactory = new TFramedTransport.Factory();
        // 使用高密度二进制协议
        TProtocolFactory proFactory = new TCompactProtocol.Factory();
        // 设置处理器 HelloImpl
        TProcessor processor = new Hello.Processor(new HelloImpl());
        // 创建服务器
        Args tArgs = new Args(serverTransport);
        tArgs.transportFactory(transportFactory);
        tArgs.protocolFactory(proFactory);
        tArgs.processor(processor);
        TServer server = new TThreadedSelectorServer(tArgs);
        System.out.println("Start server on port 7911...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) Args(org.apache.thrift.server.TThreadedSelectorServer.Args) TProcessor(org.apache.thrift.TProcessor) HelloImpl(org.tech.model.impl.HelloImpl) TProcessor(org.apache.thrift.TProcessor) TServer(org.apache.thrift.server.TServer) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) TThreadedSelectorServer(org.apache.thrift.server.TThreadedSelectorServer) TTransportFactory(org.apache.thrift.transport.TTransportFactory) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TTransportFactory(org.apache.thrift.transport.TTransportFactory)

Example 33 with TServer

use of org.apache.thrift.server.TServer in project tech by ffyyhh995511.

the class ThriftDemo method nioServer.

/**
 * 基于非阻塞IO(NIO)的服务端
 */
public void nioServer() {
    try {
        // 传输通道 - 非阻塞方式
        TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
        // 异步IO,需要使用TFramedTransport,它将分块缓存读取。
        TTransportFactory transportFactory = new TFramedTransport.Factory();
        // 使用高密度二进制协议
        TProtocolFactory proFactory = new TCompactProtocol.Factory();
        // 设置处理器 HelloImpl
        TProcessor processor = new Hello.Processor(new HelloImpl());
        // 创建服务器
        Args tArgs = new Args(serverTransport);
        tArgs.transportFactory(transportFactory);
        tArgs.protocolFactory(proFactory);
        tArgs.processor(processor);
        TServer server = new TThreadedSelectorServer(tArgs);
        System.out.println("Start server on port 7911...");
        server.serve();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TNonblockingServerTransport(org.apache.thrift.transport.TNonblockingServerTransport) Args(org.apache.thrift.server.TThreadedSelectorServer.Args) TProcessor(org.apache.thrift.TProcessor) HelloImpl(org.tech.model.impl.HelloImpl) TProcessor(org.apache.thrift.TProcessor) TServer(org.apache.thrift.server.TServer) TNonblockingServerSocket(org.apache.thrift.transport.TNonblockingServerSocket) TThreadedSelectorServer(org.apache.thrift.server.TThreadedSelectorServer) TProtocolFactory(org.apache.thrift.protocol.TProtocolFactory) TTransportFactory(org.apache.thrift.transport.TTransportFactory) TTransportFactory(org.apache.thrift.transport.TTransportFactory)

Aggregations

TServer (org.apache.thrift.server.TServer)33 TServerSocket (org.apache.thrift.transport.TServerSocket)16 TProcessor (org.apache.thrift.TProcessor)12 TNonblockingServerSocket (org.apache.thrift.transport.TNonblockingServerSocket)11 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)9 TThreadPoolServer (org.apache.thrift.server.TThreadPoolServer)9 Test (org.junit.Test)9 TProtocolFactory (org.apache.thrift.protocol.TProtocolFactory)8 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)7 TThreadedSelectorServer (org.apache.thrift.server.TThreadedSelectorServer)7 TServerTransport (org.apache.thrift.transport.TServerTransport)7 TTransportFactory (org.apache.thrift.transport.TTransportFactory)7 InetSocketAddress (java.net.InetSocketAddress)6 TFramedTransport (org.apache.thrift.transport.TFramedTransport)6 ConfigurationCopy (org.apache.accumulo.core.conf.ConfigurationCopy)5 ServerAddress (org.apache.accumulo.server.rpc.ServerAddress)5 TProtocol (org.apache.thrift.protocol.TProtocol)5 TTransport (org.apache.thrift.transport.TTransport)5 IOException (java.io.IOException)4 ServerSocket (java.net.ServerSocket)4