Netty ServerSocket 和子节点正常关闭
Netty ServerSocket and Children Graceful Shutdown
我想关闭 Netty 服务器上的服务器套接字,然后在执行资源清理之前等待所有子进程完成处理。
步骤 1
使用此代码等待 Netty 服务器套接字在主线程上关闭,其中 b
是 ServerBootstrap
的一个实例
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 user guide 看来,只需等到 shutdownGracefully
returns 就会等到所有套接字都关闭。
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.
我想关闭 Netty 服务器上的服务器套接字,然后在执行资源清理之前等待所有子进程完成处理。
步骤 1
使用此代码等待 Netty 服务器套接字在主线程上关闭,其中 b
是 ServerBootstrap
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 user guide 看来,只需等到 shutdownGracefully
returns 就会等到所有套接字都关闭。
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.