netty ChannelInboundHandlerAdapter 使用注意事项

来源:互联网 发布:js组件开发设计 编辑:程序博客网 时间:2024/06/15 05:27

场景

下面是一段普通的channelHandler处理程序,主要是继承了ChannelInboundHandlerAdapter确是隐藏了巨大的错误

““
public class ServerBizHandler extends ChannelInboundHandlerAdapter {
private ChannelHandlerContext ctx;

@Overridepublic void channelRegistered(ChannelHandlerContext ctx) throws Exception {    this.ctx = ctx;}@Overridepublic 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); }@Overridepublic void channelReadComplete(ChannelHandlerContext ctx) throws Exception {    ctx.flush();}@Overridepublic 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()即可
原创粉丝点击