2013-03-15 63 views
0

以下是我如何實施thrift服務器,但它在serve()調用後不會返回到主Therad。Thrift TServer服務不返回主線程

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(); 
    } 
    } 
} 

回答

2

你調用start()啓動線程(這將創造一個新的線程,然後調用該對象從這個新的線程run()功能),而不是run()(這隻會從同一個線程中運行run()現在在),對吧?

serve()是不是要返回,除非你停止服務器(從另一個線程)。