Search in sources :

Example 1 with HelloImpl

use of org.tech.model.impl.HelloImpl 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 2 with HelloImpl

use of org.tech.model.impl.HelloImpl 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)

Example 3 with HelloImpl

use of org.tech.model.impl.HelloImpl 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 4 with HelloImpl

use of org.tech.model.impl.HelloImpl 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)

Aggregations

TProcessor (org.apache.thrift.TProcessor)4 HelloImpl (org.tech.model.impl.HelloImpl)4 TProtocolFactory (org.apache.thrift.protocol.TProtocolFactory)3 TServer (org.apache.thrift.server.TServer)3 TThreadedSelectorServer (org.apache.thrift.server.TThreadedSelectorServer)3 TNonblockingServerSocket (org.apache.thrift.transport.TNonblockingServerSocket)3 TNonblockingServerTransport (org.apache.thrift.transport.TNonblockingServerTransport)3 Args (org.apache.thrift.server.TThreadedSelectorServer.Args)2 TTransportFactory (org.apache.thrift.transport.TTransportFactory)2 ExecutorService (java.util.concurrent.ExecutorService)1 TProcessorFactory (org.apache.thrift.TProcessorFactory)1 TCompactProtocol (org.apache.thrift.protocol.TCompactProtocol)1 TThreadPoolServer (org.apache.thrift.server.TThreadPoolServer)1 Args (org.apache.thrift.server.TThreadPoolServer.Args)1 TFramedTransport (org.apache.thrift.transport.TFramedTransport)1 TServerSocket (org.apache.thrift.transport.TServerSocket)1 TServerTransport (org.apache.thrift.transport.TServerTransport)1 Hello (org.tech.model.Hello)1