场景
下面是一段普通的channelHandler处理程序,主要是继承了ChannelInboundHandlerAdapter
确是隐藏了巨大的错误
““
public class ServerBizHandler extends ChannelInboundHandlerAdapter {
private ChannelHandlerContext ctx;
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
this.ctx = ctx;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
log.debug("thread.name={}", Thread.currentThread().getName());
ByteBuf in = (ByteBuf) msg;
String readStr = in.toString(CharsetUtil.UTF_8);
log.debug("Server received: {}", readStr);
log.debug("release msg");
ReferenceCountUtil.release(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
““
先看下堆栈异常
io.netty.util.IllegalReferenceCountException: refCnt: 0
at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1173)
at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1119)
at io.netty.buffer.UnpooledUnsafeDirectByteBuf.internalNioBuffer(UnpooledUnsafeDirectByteBuf.java:385)
at io.netty.buffer.ByteBufUtil.decodeString(ByteBufUtil.java:568)
at io.netty.buffer.AbstractByteBuf.toString(AbstractByteBuf.java:979)
at io.netty.buffer.AbstractByteBuf.toString(AbstractByteBuf.java:974)
at com.spy.apollo.netty.demo.demo02_biz_logic.ServerBizHandler.channelRead(ServerBizHandler.java:50)
异常说是引用计数为0,也就是没有被引用,因此报错;常规的channelRead中消息读取完毕是要立即释放当前消息的引用计数即(减一操作)
ReferenceCountUtil.release(msg);
分析
通过调试代码发现根源就在super.channelRead(ctx, msg);
这个函数
其实ChannelInboundHandlerAdapter 是提供了一种实现而已,子类如果要继承,需要覆盖父类中的方法,并且不需要调用super.xxxxMethod()
源码部分
再看看javadoc中ChannelInboundHandlerAdapter
是怎么解释的
结论
- 最简单的方式,使用
ChannelInboundHandler
- 使用
ChannelInboundHandlerAdapter
时 不需要调用super.xxxMethod()即可