簡體   English   中英

Netty ServerSocket 和 Children Graceful Shutdown

[英]Netty ServerSocket and Children Graceful Shutdown

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

步驟 1使用此代碼等待 Netty 服務器套接字在主線程上關閉,其中bServerBootstrap的實例

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

步驟 2指示服務器套接字從應用程序中的其他位置的不同線程關閉

步驟 3指示所有客戶端關閉,我將擁有執行此操作的應用程序代碼

第 4 步等待所有客戶端套接字關閉,這就是我想知道如何做的 如何獲取所有客戶端套接字、服務器子級的列表,然后等待所有這些套接字關閉?

Netty 版本4.1.1.Final

我使用以下代碼片段設置 Netty 服務器套接字

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

netty 4.x 用戶指南看來,只是等待shutdownGracefully返回將等到所有套接字都關閉。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM