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()即可
- netty ChannelInboundHandlerAdapter 使用注意事项
- Netty:ChannelInboundHandlerAdapter和ChannelOutboundHandlerAdapter
- netty 使用注意事项
- netty源码分析之-SimpleChannelInboundHandler与ChannelInboundHandlerAdapter详解(9)
- ChannelInboundHandlerAdapter和SimpleChannelInboundHandler的使用区分
- Netty学习总结(6)——Netty使用注意事项
- Netty ByteBuf 释放注意事项
- SimpleChannelInboundHandler和ChannelInboundHandlerAdapter区别
- SimpleChannelInboundHandler和ChannelInboundHandlerAdapter区别
- Netty 之 Netty使用Google的ProtoBuf
- Netty使用初步
- netty初步使用
- netty初步使用2
- Netty API使用总结
- netty 入门使用
- Netty使用初步
- Netty的使用模式
- Netty使用初步
- oracle数据库
- 算法系列——Implement strStr()
- Swift学习之
- tomcat 线程模型
- Maven 搭建ssh项目 pom.xml
- netty ChannelInboundHandlerAdapter 使用注意事项
- 笔记4
- 交叉编译的参数简述--build、host和target
- CSS笔记
- [CodePush] Unable to get the hash of the binary's bundled resources
- 重排链表-LintCode
- 【bzoj4414】数量积
- Android studio .aar生成及本地使用
- 使用maven完成spring aop的xml配置