gpt4 book ai didi

java - Netty ServerSocket 和子进程正常关闭

转载 作者:行者123 更新时间:2023-11-30 07:19:03 27 4
gpt4 key购买 nike

我想关闭 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 user guide看来只要等到 shutdownGraceively 返回就会等到所有套接字都关闭。

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.

关于java - Netty ServerSocket 和子进程正常关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37902844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com