Netty4 SEDA事件驱动原理分析
AioServerSocketChannel的事件处理线程(AioEventLoop)建立过程分析
(Bootstrap启动ServerSocketChannel处理线程的过程分析)
1:AbstractBootstrap.initAndRegister():向BossGroup(AioEventLoopGroup)
注册初始化后的AioServerSocketChannel,
![](https://i-blog.csdnimg.cn/blog_migrate/6b3f061dad48c319d1239bf548e7a89d.jpeg)
2,调用MultithreadEventExecutorGroup.next()方法,在BossGroup中取出一个可用的EventLoop
![](https://i-blog.csdnimg.cn/blog_migrate/e924f9dc5b3231eec6ba69747f6f61e0.jpeg)
3,返回到MultithreadEventLoopGroup.register(Channel channel, ChannelPromise promise)方法,
将AioServerSocketChannel注册到(2)从BossGroup中取出的那个EventLoop.
![](https://i-blog.csdnimg.cn/blog_migrate/1b0da6fea9a87fa4978676afebea59d6.jpeg)
4,进入到AioEventLoop.register(Channel channel, ChannelPromise future)方法
AioEventLoop实现了JDK的ScheduledExecutorService,持有一条线程,具备任务执行能力,
将AioServerSocketChannel测试到AioEventLoop后,由AioEventLoop执行ServerSocketChannel的accept新的socket的操作
![](https://i-blog.csdnimg.cn/blog_migrate/0fd7d62b46233bc90b6c834523e33def.jpeg)
5,进入到AbstractChannel.AbstractUnsafe.register(EventLoop eventLoop, ChannelPromise promise)方法,
![](https://i-blog.csdnimg.cn/blog_migrate/b524042e2e781d87591f604a65458c30.jpeg)
6)进入到SingleThreadEventExecutor.execute(Runnable task)方法,执行startThread()后,AioServerSocketChannel的事件处理线程(AioEventLoop)启动,将由它来接管AioServerSocketChanel的事件处理(如Accept新的Socket),然后ServerBootstrap线程就进入等待状态,等待ServerSocketChannel绑定端口,然后等待ServerSocketChannel关闭.
![](https://i-blog.csdnimg.cn/blog_migrate/3be20410b814f01f47c07e4ce0662995.jpeg)
7)先看下面
![](https://i-blog.csdnimg.cn/blog_migrate/286ad28073b961690199eac5c1a760bd.png)
![](https://i-blog.csdnimg.cn/blog_migrate/90e52d2dd720c4e9e280c1372b3f871b.jpeg)
8)AioServerSocketChannel的事件处理线程AioEventLoop从一个BlockingDeque里取出新任务,取出的第一个任务是给AioServerSocketChannel注册JDK的AsynchronousServerSocketChannel(这才是真正和底层操作系统通信的ServerSocketChannel)
![](https://i-blog.csdnimg.cn/blog_migrate/63742c6f09948867dc2709cb7b78001f.jpeg)
注册了:
![](https://i-blog.csdnimg.cn/blog_migrate/9ad4c41bfb5f9b73582b44d147ed5055.jpeg)
9)Channel成功注册后,调用其pipeline.fireChannelRegistered()方法Fire这个channel注册的事件
![](https://i-blog.csdnimg.cn/blog_migrate/552a4534ef8fdb9a60da227c5ff30567.jpeg)
10)ChannelPipeline的fireChannelRegistered方法先初始化其处理器链的第一个元素HeadHandler,然后再执行HeadHandler的fireChannelRegistered()方法
![](https://i-blog.csdnimg.cn/blog_migrate/8da5e6c111e1fa2624a214f1f6beb29e.jpeg)
DefaultChannelHandlerContext.fireChannelRegistered(),Netty4的DefaultChannelHandlerContext对ChannelInboundInvoker接口的的事件处理方法fireXxxx(),具有链式传递效应,顺序将同一方向上的所有事件
![](https://i-blog.csdnimg.cn/blog_migrate/8538c132017266139b42c6d4faae903f.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/d27bcf5dc18d3a2c15c33f87c76246c5.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/4caaa38b7987fc8237addfbc11da46d9.jpeg)
11)然后,AioServerSocketChanel的pipeline的处理器链上的ChannelInitializer响应channelRegistered事件:给pipeline安装上Acceptor,使得AioServerSocketChanel得以具备处理socket Accept事件的能力.
![](https://i-blog.csdnimg.cn/blog_migrate/a63c57d37fad8608323b2348d71e716d.jpeg)
12) AioServerSocketChanel将accept委托给JDK的异步ServerSocketChannel来执行异步accept,并指定了事件处理器,当ACCEPT事件发生的时候, AcceptHandler就会被调用
![](https://i-blog.csdnimg.cn/blog_migrate/39caea8ec551e8d90f0d5ce9e9376ba3.png)
然后AioServerSocketChanel的AioEventLoop就进入等待状态,等待taskQueue有任务到来
13)现在在浏览器输入http://127.0.0.1:8080/,向服务器发出请求
当相应事件发生时,JDK的CompletionHandler.complted(..)方法会被一条后台Daemon的线程调用:
![](https://i-blog.csdnimg.cn/blog_migrate/1e8412918ba393cc26b3d5404dc22a4d.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/b35674a1d2e765f47c73b9d40f9e5ffb.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/ec09dbe7f1fa24ff41891c8d4e7d9899.jpeg)
将接收到的SocketChannel包装成AioSocketChannel作为一个Message写进AioServerSocketChannel的ChannelPipeline的inboundMessageBuffer,然后Fire相关事件inboundBufferUpdated事件
14)AioServerSocketChannel的ChannelPipeline Fire inboundBufferUpdated事件
![](https://i-blog.csdnimg.cn/blog_migrate/c5ecbe18b0b5f89764acc4722717d409.png)
15)沿inbound方向找到了SererBootstrapAcceptor的ChannelHandlerContext
![](https://i-blog.csdnimg.cn/blog_migrate/edf9e284ec3ea49ce8150168ef1412d0.jpeg)
16)inboundBufferUpdate事件传递到ServerBootstrapAcceptor
![](https://i-blog.csdnimg.cn/blog_migrate/ac41f05cdc4290ca2036a775bebd1270.jpeg)
17)DefaultChannelHandlerContext.invokeInboundBufferUpdated(),调用Acceptor的inboundBufferUpdated方法
![](https://i-blog.csdnimg.cn/blog_migrate/8103be9fb6bf564ed1a99235ddf0f36c.jpeg)
18)Acceptor取得AioSocketChannel,并向其添加ChannelInitializer
![](https://i-blog.csdnimg.cn/blog_migrate/501d520b22b23b88c70200d384673d73.jpeg)
19)向ChildGroup注册AioSocketChannel
![](https://i-blog.csdnimg.cn/blog_migrate/47c30f44d0ad8b2a5156abc94f5d10f4.jpeg)
20)将AioSocketChannel注册到AioEventLoopGroup分配的AioEventLoop上
![](https://i-blog.csdnimg.cn/blog_migrate/007ca78451346e70e13493b2b2340493.jpeg)
21)BOSS线程(AioServerSocketChannel所属的线程)启动AioSocketChannel所属的AioEventLoop线程,BOSS线程的Accept工作也至此结束,然后就去回去处理其taskQueue中的任务,或进入wait状态,等待下一个Accept事件的发生.
![](https://i-blog.csdnimg.cn/blog_migrate/57b86c14db14a61ffa09a0252483ff84.jpeg)
Worker线程线程的初始化
22)Worker线程(AioSocketChannel所属的AioEventLoop)独立运行后,取出其taskQueue中的第一个任务:通过AioSocketChannel的ChannelPipeline触发pipeline的ChannelHandlerContext处理器链的channelRegistered注册事件
![](https://i-blog.csdnimg.cn/blog_migrate/c146eec18e0463826837ec9efe905f7b.jpeg)
23)初始化ByteHeadHandler(ChannelOutboundHandler的子类),给它分配outByteBuf和outMsgBuf
![](https://i-blog.csdnimg.cn/blog_migrate/f697adc660f3d5fd1a796f9c313deeca.jpeg)
24)触发处理器链的下一节点的channelRegistered事件
![](https://i-blog.csdnimg.cn/blog_migrate/2f7e5101e640aacdcec3f13255aad214.jpeg)
25)沿ChannelHandlerContext处理器链找到了自己定义的用户AioSocketChannel初始化的HttpSnoopServerInitializer
![](https://i-blog.csdnimg.cn/blog_migrate/4ec06c42038a86799c84e4a505fa0461.jpeg)
26)ChannelInitializer是利用channelRegistered事件来初始化pipeline,向其安装相关channelHandler
![](https://i-blog.csdnimg.cn/blog_migrate/26d687fa9ebfbf2f78f2e5825b9a022f.jpeg)
27)从pipeline的处理器链删除初始化器,并触发下一处理器的channelRegistered事件
![](https://i-blog.csdnimg.cn/blog_migrate/8fabd4069723d9a76338c42daa751d61.jpeg)
28) channelRegistered事件通过一个一个ChannelHandlerContext的fireChannelRegistered方法沿ByteHeadHandler传递到TailHandler,然后事件就停止传播
![](https://i-blog.csdnimg.cn/blog_migrate/afa116f84ed15d43897da06d325cc705.jpeg)
29)ChannelPipeline完成channelRegistered的事件触发后,就触发channelActive事件,
然后channelActive事件就随着ChannelHandlerContext和链式调用而在处理器链中传递,原理上上面差不多,故不详细说.
![](https://i-blog.csdnimg.cn/blog_migrate/da10f7f56e52b94e72563d3bf6ce6b3e.jpeg)
30)AioSocketChannel读取数据
![](https://i-blog.csdnimg.cn/blog_migrate/738cf79b07dcfc8a89e9945eb27a9129.jpeg)
由pipeline来读取数据
![](https://i-blog.csdnimg.cn/blog_migrate/08f585e771f8dd82f79328a8060963d7.jpeg)
到TailHandler读取数据
![](https://i-blog.csdnimg.cn/blog_migrate/5e65fced9604321499750c1ca39412b3.jpeg)
31)向outbound方向上寻找最近的ChannelOperationHandler,委托它进行数据读取
![](https://i-blog.csdnimg.cn/blog_migrate/570b3d5c1bada13f83084945a3f2a24e.jpeg)
首先委托给HttpResponseEncoder读取
![](https://i-blog.csdnimg.cn/blog_migrate/73c6d64ee62b8990b46c2027469f3e19.jpeg)
HttpRequestDecoder委托给下一个ChannelOperationHandler读取
![](https://i-blog.csdnimg.cn/blog_migrate/54483865ff2d0d2ecb158f7761581c93.jpeg)
最终委托给ByteHeadHandler读取
![](https://i-blog.csdnimg.cn/blog_migrate/247dd2d5133e8d532f0e6b8daba1ef4e.jpeg)
32)最终由AioSocketChannel通过JDK的AsynchronousSocketChannel从底层读取
![](https://i-blog.csdnimg.cn/blog_migrate/47590a46af1410fd4442918ea6628cba.jpeg)
33)AioSocketChannel将读取数据操作委托给JDK的AsynchronousSocketChannel来完成,AioSocketChannel的当前线程不会被阻塞,立马返回处理taskQueue中下一任务或没任务时进入wait等待BlockingQueue的状态.
AsynchronousSocketChannel的read操作完成后,ReadHandler会被一条后台Daemon线程调用
![](https://i-blog.csdnimg.cn/blog_migrate/c212cc1520fbf4c971c835200d77fe94.jpeg)
34)ReadHandler的执行从Daemon线程转移回AioSocketChannle所属的AioEventLoop线程来执行
![](https://i-blog.csdnimg.cn/blog_migrate/9f19db946413a1fa34bf713c19b7bfb4.jpeg)
35)由AioEventLoop执行ReadHandler
![](https://i-blog.csdnimg.cn/blog_migrate/a6e4993d38aa4907a2bc4d5c0df2b575.jpeg)
36)数据已准备好,pipeline触发inboundBufferUpdated方法,然后pipeline中处理器链的inbound方向上的ChannelStateHandler的inboundBufferUpdated方法会被顺度调用
![](https://i-blog.csdnimg.cn/blog_migrate/1e1400c5277d1ec0069cca6d651744ca.jpeg)
37)head触发处理器链上inbound方向上的stateHandler的inboudBufferUpdated方法的顺序调用
![](https://i-blog.csdnimg.cn/blog_migrate/34dee28c4beddd35d46248f2a5050452.jpeg)
38)处理器链上inbound方向上的stateHandler的inboudBufferUpdated方法的顺序调用
-----这里省略,参考:http://blog.csdn.net/irelandken/article/details/8760302
39)输入数据:DefaultChannelHandlerContext.flush(ChannelPromise promise)
![](https://i-blog.csdnimg.cn/blog_migrate/b216a1b53180ef52f05b57f7fd30ccf0.jpeg)
40)由HttpServerCodec中的HttpResponseEncoder将HttpMessage转换为ByteBuf
DefaultChannelHandlerContext.invokeFlush0(ChannelPromise promise)
![](https://i-blog.csdnimg.cn/blog_migrate/48fafed1c83a275917054e960c6ac572.jpeg)
41)由Pipeline的ByteHeadHandler将ByteBuf flush出去
![](https://i-blog.csdnimg.cn/blog_migrate/a0d323b9bfd1a8da05670a986167e1f0.jpeg)
42)AioSocketChannel将Flush操作委托给JDK AsyncounsSocketChannel异步进行,最终结果等写操作完成后由后台Deamon线程通知并执行WriteHandler.
而AioEventLoop线程是马上返回的,不会被write操作阻塞,然后继续执行taskQueue中的任务或进入wait状态等待BlockingQueue.
![](https://i-blog.csdnimg.cn/blog_migrate/a1dac2457ce8277f0bf9a2b8713c18d5.jpeg)
43)Write操作完成后,WriteHandler被后台Daemon线程调用
![](https://i-blog.csdnimg.cn/blog_migrate/5b13746e2039b5fb6456e0f69df1b7f5.jpeg)
44)再次WriteHandler转交由关联的AioSocketChannel所属的AioEventLoop执行
![](https://i-blog.csdnimg.cn/blog_migrate/02d49ef98415e481a3a90445be6ce108.jpeg)
45) AioEventLoop执行完WriteHandler后,整个接收和响应过程就完成了.