Netty学习-handler 的基类选择!
来源:互联网 发布:图片转换矢量图软件 编辑:程序博客网 时间:2024/06/05 03:32
netty 作为 rpc框架,其nio的特性很好,目前再看《Netty权威指南》书籍,下载了书中的例子照做,发现有些程序运行不了,通过解决分析,记录如下。
是netty 的入门例子,
我使用的版本是,
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.9.Final</version></dependency>
作者写的书时候 版本肯定比这老,估计 ChannelHandlerAdapter是 包含了对io 事件的触发,但是我运行这个例子,始终运行不起来。
然后分析源码如下。
public abstract class ChannelHandlerAdapter implements ChannelHandler { boolean added; public ChannelHandlerAdapter() { } protected void ensureNotSharable() { if(this.isSharable()) { throw new IllegalStateException("ChannelHandler " + this.getClass().getName() + " is not allowed to be shared"); } } public boolean isSharable() { Class clazz = this.getClass(); Map cache = InternalThreadLocalMap.get().handlerSharableCache(); Boolean sharable = (Boolean)cache.get(clazz); if(sharable == null) { sharable = Boolean.valueOf(clazz.isAnnotationPresent(Sharable.class)); cache.put(clazz, sharable); } return sharable.booleanValue(); } public void handlerAdded(ChannelHandlerContext ctx) throws Exception { } public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { } public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.fireExceptionCaught(cause); }}
public interface ChannelHandler { void handlerAdded(ChannelHandlerContext var1) throws Exception; void handlerRemoved(ChannelHandlerContext var1) throws Exception; /** @deprecated */ @Deprecated void exceptionCaught(ChannelHandlerContext var1, Throwable var2) throws Exception; @Inherited @Documented @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Sharable { }}
可以看到 ChannelHandlerAdapter,ChannelHandler 都没有包含对IO 事件的触发。所以我运行实例后,始终没有任何反应!感觉服务器和客户端没有发现通信!
既然找到了原因,那么解决也就简单了。
public class ChannelDuplexHandler extends ChannelInboundHandlerAdapter implements ChannelOutboundHandler {
public class ChannelInboundHandlerAdapter extends ChannelHandlerAdapter implements ChannelInboundHandler { public ChannelInboundHandlerAdapter() { } public void channelRegistered(ChannelHandlerContext ctx) throws Exception { ctx.fireChannelRegistered(); } public void channelUnregistered(ChannelHandlerContext ctx) throws Exception { ctx.fireChannelUnregistered(); } public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.fireChannelActive(); } public void channelInactive(ChannelHandlerContext ctx) throws Exception { ctx.fireChannelInactive(); } public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ctx.fireChannelRead(msg); } public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.fireChannelReadComplete(); } public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { ctx.fireUserEventTriggered(evt); } public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { ctx.fireChannelWritabilityChanged(); } public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.fireExceptionCaught(cause); }}
这个ChannelDuplexHandler 可以即包含了对 io 事件(inbound) 又包含了对 用户事件(outbound)的触发。所以使用这个就可以完成运行!
0 0
- Netty学习-handler 的基类选择!
- netty 学习 (2)Handler的执行顺序
- netty 学习 (2)Handler的执行顺序
- netty 学习 (2)Handler的执行顺序
- netty 学习 (2)Handler的执行顺序
- nio学习之netty入门(2)---netty中handler的执行顺序
- Netty学习(一)-为什么选择Netty
- Netty:Handler的执行顺序
- 选择Netty的理由
- Mina和Netty的选择
- Netty:Bootstrap的handler和childHandler
- Netty:Bootstrap的handler和childHandler
- Jboss .netty项目使用的netty包详解之 org.jboss.netty.handler.codec.base64
- netty的学习记录
- Netty学习(八)-Netty的心跳机制
- 《Netty 权威指南》—— 选择Netty的理由
- netty 学习 (4)混合使用coder和handler
- Netty超时控制handler
- dp斜率优化 Pearls(Hdu1300)题解
- opencv配置
- 一张图明白box-sizing属性
- JVM系列三:JVM参数设置、分析
- Python的并列和或者条件
- Netty学习-handler 的基类选择!
- Struts优缺点
- POJ 3126 Prime Path
- virtualbox 中原来系统的磁盘文件vdi复制及移动
- 微信的智能心跳方案
- VR实训项目规划(3D场景篇)
- 交流心得1
- java string类型转换boolean类型
- 计算机网络之Cisco Packet Tracer模拟器使用