简体   繁体   中英

Netty ServerSocket and Children Graceful Shutdown

I would like to close the server socket on a Netty server and then wait for all children to finish processing before executing resource cleanup.

Step 1 Wait for the Netty server socket to close on the main thread using this code where b is an instance of ServerBootstrap

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

Step 2 Instruct the server socket to close from a different thread, somewhere else in the application

Step 3 Instruct all clients to shutdown, I will have application code that will do this

Step 4 Wait for all client sockets to close, this is what I want to know how to do . How can I get a list of all client sockets, children of the server, and then wait for all of them to close?

Netty version 4.1.1.Final

I setup the Netty server socket using the following code snippet

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

From the netty 4.x user guide it appears that simply waiting until the shutdownGracefully returns will wait until all sockets are closed.

Shutting down a Netty application is usually as simple as shutting down all EventLoopGroups you created via shutdownGracefully(). It returns a Future that notifies you when the EventLoopGroup has been terminated completely and all Channels that belong to the group have been closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM