Java 内蒂没有';t在通道断开后检测超时
奇怪的网络行为:获得了一个服务器通道,该通道期望心跳并通过Java 内蒂没有';t在通道断开后检测超时,java,timeout,netty,heartbeat,Java,Timeout,Netty,Heartbeat,奇怪的网络行为:获得了一个服务器通道,该通道期望心跳并通过IdleStateHandler捕获超时。类似这样的构造: @Override public void initChannel(SocketChannel ch) throws Exception { final ByteBuf delimiter = Unpooled.buffer(1); long serverTimeout = 1000; // Break input by newlines:
IdleStateHandler
捕获超时。类似这样的构造:
@Override
public void initChannel(SocketChannel ch) throws Exception {
final ByteBuf delimiter = Unpooled.buffer(1);
long serverTimeout = 1000;
// Break input by newlines:
delimiter.writeByte('\n');
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(32768, delimiter));
// Use UTF8 string:
ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
// Timeout
ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(serverTimeout, 0, 0, TimeUnit.MILLISECONDS));
// Handle commands:
ch.pipeline().addLast("myHandler", new DBConnectionHandler());
}
如果客户端无法发送心跳,一切正常——超时被触发并处理。然而,如果我残忍地杀死客户端,相当于断开电缆,Netty不会触发超时
使用Netty的超时异常(而不是idle事件)会得到相同的结果 事实证明,Netty确实识别了通道丢失,只是没有触发死区通道的超时。检测相关频道上下文的调用
channelHandlerContext.isRemoved()
我认为在这里检测channelInactive(…)更为正确。