TThreadedSelectorServer is the most mature and respected RPC service model in the industry. TThreadedSelectorServer is an extension of the above NonblockingServer, which separates the Selector threads of Accept and Read/Write, and introduces the Worker worker thread pool. It is also a Half-sync/Half-async service model.
MainReactor is the Accept thread, which is used to monitor client connections. SubReactor uses IO event threads (multiple), which are mainly responsible for processing IO read and write events of all clients. The Worker worker thread is mainly used to process the handler callback of each rpc request Processing (this part is synchronous).
Let's look at the implementation of the server:
- package cn.slimsmart.thrift.demo.helloworld;
-
- import org.apache.thrift.TException;
- import org.apache.thrift.TProcessor;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.server.TServer;
- import org.apache.thrift.server.TThreadedSelectorServer;
- import org.apache.thrift.transport.TFramedTransport;
- import org.apache.thrift.transport.TNonblockingServerSocket;
-
-
-
-
-
- publicclass HelloTThreadedSelectorServer {
-
-
- publicstaticfinalint SERVER_PORT = 8080;
-
- publicstaticvoid main(String[] args) throws TException {
-
- TProcessor tprocessor = new HelloWorld.Processor<HelloWorld.Iface>(new HelloWorldImpl());
-
- TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(SERVER_PORT);
-
-
- TThreadedSelectorServer.Args tArgs = new TThreadedSelectorServer.Args(serverTransport);
- tArgs.processor(tprocessor);
- tArgs.transportFactory(new TFramedTransport.Factory());
-
- tArgs.protocolFactory(new TBinaryProtocol.Factory());
-
- TServer server = new TThreadedSelectorServer(tArgs);
- System.out.println("HelloTThreadedSelectorServer start....");
- server.serve();
-
- }
-
- }
http://blog.csdn.net/zhu_tianwei/article/details/44115297