Thrift TServer服务不会返回主线程

时间:2013-03-15 17:03:37

标签: java thrift

以下是我如何实现thrift服务器,但在serve()调用后它不会返回主therad。

public class ThriftServerRunner implements Runnable {
    private int thriftServerPort;
    public ThriftServerRunner(int thriftServerPort, LogWriter logWriter) {
        this.thriftServerPort = thriftServerPort;   
    }
    @Override
    public void run() {
        try {
            SetupThriftServer();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }               
    }

    private void SetupThriftServer() throws Exception {
    try {
            TServerSocket serverTransport = new TServerSocket(this.thriftServerPort);
            ThriftService.Processor<ThriftService.Iface> processor = new ThriftService.Processor(new ThriftServiceImpl());
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
        server.serve();      
    } catch (TTransportException e) {
            e.printStackTrace();
    }
    }
}

1 个答案:

答案 0 :(得分:2)

你调用start()来启动线程(它将创建一个新线程,然后从这个新线程调用对象run()函数),而不是run()(这将只是从你当前所在的同一个线程中运行run(),对吗?

serve()并不意味着返回,除非你停止服务器(来自另一个线程)。