2016-06-19 32 views
1

我想關閉Netty服務器上的服務器套接字,然後等待所有孩子在執行資源清理之前完成處理。Netty ServerSocket和Children Graceful Shutdown

步驟1 等待的Netty服務器套接字使用這個代碼,其中bServerBootstrap

b.bind (getPort (p)).sync().channel().closeFuture().sync() 

步驟2 一個實例指示服務器套接字從關閉以關閉主線程上一個不同的線程,在應用程序的其他地方

步驟3 個指示所有的客戶端關機,我將應用程序代碼,將做到這

步驟4 等待所有客戶端套接字關閉,這是我想知道怎麼做什麼。我如何獲得所有客戶端套接字,服務器的子進程列表,然後等待它們全部關閉?

Netty的版本4.1.1.Final

我安裝使用下面的代碼片段

EventLoopGroup bossGroup = new NioEventLoopGroup (1); 
EventLoopGroup workerGroup = new NioEventLoopGroup (Math.max (1, Runtime.getRuntime().availableProcessors()/2)); 
try 
{ 
    ServerBootstrap b = new ServerBootstrap(); 
    b.group (bossGroup, workerGroup) 
    .channel (NioServerSocketChannel.class) 
    .childHandler (new ServerInitializer (sslctx, jsonIOManager, dispatcherPool, factory)) 
    .childOption (ChannelOption.TCP_NODELAY, Boolean.TRUE) 
    .childOption (ChannelOption.SO_KEEPALIVE, Boolean.TRUE); 

    // wait till server socket is closed  
    b.bind (getPort (p)).sync().channel().closeFuture().sync(); 
    // instruct all clients to shutdown 
    dispatcherPool.shutdown(); 

    // wait on all sockets that are children of the server socket to close 
    // How can i do this? 

} 
finally 
{ 
    bossGroup.shutdownGracefully(); 
    workerGroup.shutdownGracefully(); 
} 

回答

1

從似乎只是在等待,直到shutdownGracefully回報將等到netty 4.x user guide的Netty的服務器套接字所有插座都關閉。

關閉一個應用程序的Netty通常是關閉 下來,你通過shutdownGracefully創建的所有EventLoopGroups一樣簡單()。它 返回一個Future,當EventLoopGroup已被完全終止並且所有屬於該組的通道已關閉 時通知您。