title: Netty ServerSocket and Children Graceful Shutdown
body: <p>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.</p>
  
  <p><strong>Step 1</strong>
  Wait for the Netty server socket to close on the main thread using this code where <code>b</code> is an instance of <code>ServerBootstrap</code></p>
  
  <pre><code>b.bind ( getPort ( p ) ).sync ().channel ().closeFuture ().sync ()
  </code></pre>
  
  <p><strong>Step 2</strong>
  Instruct the server socket to close from a different thread, somewhere else in the application</p>
  
  <p><strong>Step 3</strong>
  Instruct all clients to shutdown, I will have application code that will do this</p>
  
  <p><strong>Step 4</strong>
  Wait for all client sockets to close, <strong>this is what I want to know how to do</strong>. How can I get a list of all client sockets, children of the server, and then wait for all of them to close?</p>
  
  <p>Netty version <strong>4.1.1.Final</strong></p>
  
  <p>I setup the Netty server socket using the following code snippet</p>
  
  <pre><code>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 ();
  }
  </code></pre>