netty之SimpleChannelInboundHandler
来源:互联网 发布:视频剪辑专业软件 编辑:程序博客网 时间:2024/06/06 04:20
netty之SimpleChannelInboundHandler
在最开始学些netty的时候,写的服务端demo的handler,继承的都是ChannelInboundHandlerAdapter,客户端继承的是SimpleChannelInboundHandler,当然最开始学的时候都是在不断的写demo,并不清楚为何两边继承的类还不一样,还想过服务端handler也继承SimpleChannelInboundHandler类,这样就不需要每次将数据转换成我们需要的类型了,但是后来逐渐了解用法后,便放弃了使用SimpleChannelInboundHandler作为服务端处理类。
首先说ChannelInboundHandlerAdapter类,每次使用都需要将msg转换成我们需要的类型,如下:
@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { String body = (String) msg; System.out.println("The time server receive order : " + body + "; the counter is : " + ++counter); String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER"; currentTime = currentTime + System.getProperty("line.separator"); ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes()); ctx.writeAndFlush(resp); }
如上就是在编写一个最简单的demo程序的时候将msg转化成一个字符串,然后输出字符串,但是只是这样写还是不行的,还要在pipline中添加StringDecoder的转码,如下:
protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024)); socketChannel.pipeline().addLast(new StringDecoder()); socketChannel.pipeline().addLast(new TimeServerHandler()); }
这样写完后,在运行server程序的时候,channelRead方法就会正确的执行,不加上面吃力handler,程序会channelRead方法处一直阻塞住,不会得到正确的响应。
再看SimpleChannelInboundHandler的源码的时候,class最上面的注释就是:
/** * {@link ChannelInboundHandlerAdapter} which allows to explicit only handle a specific type of messages. * * For example here is an implementation which only handle {@link String} messages. * * <pre> * public class StringHandler extends * {@link SimpleChannelInboundHandler}<{@link String}> { * * {@code @Override} * protected void channelRead0({@link ChannelHandlerContext} ctx, {@link String} message) * throws {@link Exception} { * System.out.println(message); * } * } * </pre> */public abstract class SimpleChannelInboundHandler<I> extends ChannelInboundHandlerAdapter {
继承ChannelInboundHandlerAdapter,泛型指定处理类型,用法在注释中写的十分清楚,因此客户端handler的写法则是继承SimpleChannelInboundHandler类后,指定处理类型,实现的方法中的参数就是我们所需的类型,十分方便。
另外一个需要知道的就是,SimpleChannelInboundHandler覆盖了channelRead方法后,释放了资源:
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { boolean release = true; try { if (acceptInboundMessage(msg)) { @SuppressWarnings("unchecked") I imsg = (I) msg; channelRead0(ctx, imsg); } else { release = false; ctx.fireChannelRead(msg); } } finally { if (autoRelease && release) { ReferenceCountUtil.release(msg); } } }
在这释放资源后,我们如果想传递到下一个handler中是没有作用的,因此最后在服务端的程序中不要使用这个类,服务端还是使用ChannelInboundHandlerAdapter,客户端继承这个类进行数据处理后无需释放掉资源,因此才知道那么多的博客上写的使用这两个类是有一定讲究的,服务端使用ChannelInboundHandlerAdapter,客户端使用SimpleChannelInboundHandler。
- netty之SimpleChannelInboundHandler
- Netty 源码分析之SimpleChannelInboundHandler
- 一起学Netty(三)之 SimpleChannelInboundHandler
- 一起学Netty(三)之 SimpleChannelInboundHandler
- 一起学Netty(三)之 SimpleChannelInboundHandler
- netty源码分析之-SimpleChannelInboundHandler与ChannelInboundHandlerAdapter详解(9)
- Netty框架SimpleChannelInboundHandler<T>主要方法详解
- Netty框架SimpleChannelInboundHandler<T>主要方法详解
- Netty中的SimpleChannelInboundHandler的通信过程
- 【Netty】Netty系列之Netty可靠性分析
- 【Netty】Netty系列之Netty线程模型
- Netty系列之Netty可靠性分析
- Netty系列之Netty安全性
- Netty系列之Netty可靠性分析
- Netty系列之Netty可靠性分析
- Netty系列之Netty可靠性分析
- Netty系列之Netty安全性
- SimpleChannelInboundHandler和ChannelInboundHandlerAdapter区别
- 2017.07.24 MyBatis基础
- vim列操作-替换
- 方差分析
- GitHub本地操作
- Hash Table原理、基本使用方法、及源码分析
- netty之SimpleChannelInboundHandler
- vim列操作-删除操作
- vim和shell交互两种方式
- 学习笔记TF030:实现AlexNet
- Nginx反向代理服务器安装与配置
- 拷贝两个文件夹中不同的内容到一个文件夹
- Mybatis基础
- 【Redis基础】事务与事件
- [蓝桥杯]十六进制转八进制