首页  

netty Event Handler 和 Pipeline     所属分类 netty 浏览量 97
pipeLine  Channel  handler  
filter 过滤器 拦截器 

channel 创建时,和它对应的ChannelPipeline也会被创建

public interface ChannelPipeline 
extends ChannelInboundInvoker, ChannelOutboundInvoker, Iterable< Map.Entry< String, ChannelHandler>>


ChannelPipeline 继承自 Iterable,可遍历的
ChannelPipeline提供了一系列的add和remote方法 ,添加或移除 Handler
filter 顺序 , addFirst 和 addLast

两种操作, 读入 Inbound, 写出 OutBound
Inbound和outbound的处理顺序是相反的

ChannelPipeline p = ...;
p.addLast("1", new InboundHandlerA());
p.addLast("2", new InboundHandlerB());
p.addLast("3", new OutboundHandlerA());
p.addLast("4", new OutboundHandlerB());
p.addLast("5", new InboundOutboundHandlerX());

向ChannelPipeline添加了5个handler,其中2个InboundHandler,2个OutboundHandler和一个同时处理In和Out的Handler

发生 inbound 事件时  执行顺序 1,2,5
发生 outbound 事件时  执行顺序 5,4,3



ChannelHandler netty 事件驱动 ,所有的event都由Handler来处理 ChannelHandler 可以处理IO、拦截IO或者将event传递给ChannelPipeline中的下一个Handler处理 ChannelHandler 只有三个方法 void handlerAdded(ChannelHandlerContext ctx) throws Exception; void handlerRemoved(ChannelHandlerContext ctx) throws Exception; void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception; ChannelHandler 分为两类, ChannelInboundHandler 和 ChannelOutboundHandler ChannelInboundHandlerAdapter ChannelOutboundHandlerAdapter ChannelDuplexHandler ChannelHandler ChannelHandlerContext ChannelPipeline ChannelHandlerContext channel() 获取绑定的channel handler() 获取绑定的Handler fire*方法 触发Channel的事件 public interface ChannelHandlerContext extends AttributeMap, ChannelInboundInvoker, ChannelOutboundInvoker 触发和传播事件 对于 Inbound ,传播事件的方法有: ChannelHandlerContext.fireChannelRegistered() ChannelHandlerContext.fireChannelActive() ChannelHandlerContext.fireChannelRead(Object) ChannelHandlerContext.fireChannelReadComplete() ChannelHandlerContext.fireExceptionCaught(Throwable) ChannelHandlerContext.fireUserEventTriggered(Object) ChannelHandlerContext.fireChannelWritabilityChanged() ChannelHandlerContext.fireChannelInactive() ChannelHandlerContext.fireChannelUnregistered() 对于 Outbound ,传播事件的方法有: ChannelHandlerContext.bind(SocketAddress, ChannelPromise) ChannelHandlerContext.connect(SocketAddress, SocketAddress, ChannelPromise) ChannelHandlerContext.write(Object, ChannelPromise) ChannelHandlerContext.flush() ChannelHandlerContext.read() ChannelHandlerContext.disconnect(ChannelPromise) ChannelHandlerContext.close(ChannelPromise) ChannelHandlerContext.deregister(ChannelPromise) 这些方法,在一个Handler中调用,然后将事件传递给下一个Handler public class ChannelInboundHandler extends ChannelInboundHandlerAdapter { @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("connected"); ctx.fireChannelActive(); } }
ChannelHandler中的状态变量 private final AttributeKey< Boolean> auth = AttributeKey.valueOf("auth"); Attribute< Boolean> attr = ctx.attr(auth); attr.set(true); ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("MyHandler1", new MyHandler1()); pipeline.addLast("MyHandler2", new MyHandler2()); pipeline.addLast("MyHandler3", new MyHandler3()); static final EventExecutorGroup group = new DefaultEventExecutorGroup(16); pipeline.addLast(group,"MyHandler3", new MyHandler3()); DefaultEventExecutorGroup 加入的 Handler 会顺序执行的 如果确实不想顺序执行,可以尝试考虑使用 UnorderedThreadPoolEventExecutor

上一篇     下一篇
Netty EventLoop

netty VS mina

热锅冷油,炒菜不粘锅的科学原理

netty中的设计模式

Java类命名建议

中证A500指数