首页 > 其他 > 详细

ChannelInboundHandlerAdapter, SimpleChannelInboundHandler<I>区别和优缺点

时间:2019-09-05 17:31:52      阅读:229      评论:0      收藏:0      [点我收藏+]

技术分享图片

看源码,推荐用ChannelInboundHandlerAdapter

https://www.rumoss.cn/archives/183/

https://stackoverflow.com/questions/36075855/multiple-channelinboundhandleradapters-in-netty-pipleline

netty发送和接收数据handler处理器 主要是继承 SimpleChannelInboundHandler 和 ChannelInboundHandlerAdapter

  一般用netty来发送和接收数据都会继承SimpleChannelInboundHandler和ChannelInboundHandlerAdapter这两个抽象类,那么这两个到底有什么区别呢?

  其实用这两个抽象类是有讲究的,在客户端的业务Handler继承的是SimpleChannelInboundHandler,而在服务器端继承的是ChannelInboundHandlerAdapter

  最主要的区别就是SimpleChannelInboundHandler在接收到数据后会自动release掉数据占用的Bytebuffer资源(自动调用Bytebuffer.release())。而为何服务器端不能用呢,因为我们想让服务器把客户端请求的数据发送回去,而服务器端有可能在channelRead方法返回前还没有写完数据,因此不能让它自动release。

handler处理器 内置 方法

channelActive

通道激活时触发,当客户端connect成功后,服务端就会接收到这个事件,从而可以把客户端的Channel记录下来,供后面复用

channelRead

当收到对方发来的数据后,就会触发,参数msg就是发来的信息,可以是基础类型,也可以是序列化的复杂对象。

channelReadComplete

channelRead执行后触发

exceptionCaught

出错时会触发,做一些错误处理

 《Netty In Action》在第2章的例子中,发现 Echo 服务端使用的ChannelHandler是 ChannelInboundHandlerAdapter ,而 Echo 客户端使用的却是 SimpleChannelInboundHandler 。

ChannelInboundHandlerAdapter, SimpleChannelInboundHandler<I>区别和优缺点

原文:https://www.cnblogs.com/CreatorKou/p/11467066.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!