このコードスニペットでは、shutdownGracefully()。syncUninterruptibly()は返されません。 これは予想される動作ですか、何か誤解しましたか? ネッティー4.0.35.FinalShutdownGracefully()が返さない
public class ShutdownGracefullyDemo {
private ServerBootstrap bootstrap;
public static void main(String[] args) throws Exception {
new ShutdownGracefullyDemo().initialize();
}
private void initialize() throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup();
bootstrap = new ServerBootstrap();
bootstrap.group(group)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new MyInboundHandler())
.bind(2025)
.sync()
.channel()
.closeFuture()
.sync();
}
@ChannelHandler.Sharable
private class MyInboundHandler extends SimpleChannelInboundHandler<ByteBuf> {
Charset charset = Charset.forName("US-ASCII");
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg)
throws Exception {
String data = msg.toString(charset);
System.out.println("Received: " + data);
if ("quit\r\n".equals(data)) {
shutdown();
}
}
}
private void shutdown() {
if (bootstrap != null && bootstrap.group() != null) {
bootstrap.group().shutdownGracefully().syncUninterruptibly();
System.out.println("shutdown completed");
}
}
}
あなたの答えはDerekに感謝します!私のスニペットではNorman Maurerの本 "Netty in action"からインスピレーションを得ました。シャットダウンの例では、 'future.syncUninterruptibly()'メソッドは 'shutdownGracefully()'の後に呼び出され、実際にはシャットダウンを呼び出し、操作が完了したら私には意味があります。しかし何らかの理由で私がうんざりしていると、誰も待っているスレッドに通知していないので、これは期待どおりに機能しません。 – staedler
サーバがすべてのクライアントチャネルを閉じるのを待っていて、デッドロックのようにこのチャネル上でタスクが実行されるため、新しいスレッドを作成してシャットダウンすることができます。 –