netty的几个核心组件
核心概念
核心组件
-
EventLoop
-
Event
-
Channel
-
ChannelPipeline
-
ChannelHandler
-
ChannelContextHandler
一、EventLoop
netty的线程模型
每个客户端的socket都会注册到一个EventLoop上,由这个EventLoop负责处理该channel的所有IO事件和IO操作。
二、 Event
netty中定义了许多事件类型,其中包括以下一种
-
ChannnelActive事件 ----------------------- 建立连接
-
ChannelInActive事件 ----------------------- 连接关闭
-
ChannelRead事件 ----------------------- 收到消息
-
ChannelReadComplete事件 ----------------------- 读完成
-
ChannelRegistered事件 ----------------------- channel被注册到EventLoop
-
ChannelUnregistered事件 ----------------------- channel从EventLoop注销
-
ChannelWritabilityChanged 事件 ----------------------- 可写状态改变
- ExceptionCaught事件 ----------------------- 出现异常
- UserEventTriggerd事件 ----------------------- 收到一个用户自定义事件
netty根据事件的类型,去调用对应的handler方法去处理该事件,每种事件都有与之对应的处理方法。比如:ChannelRead事件对应的handler内部的处理方法是:channelRead()方法。
这有点类似于springmvc,在一个controller下面有多个接口方法,根据不同的url,为之寻找到
处理该url请求的conroller里面的方法。
如何在handler之间传递事件:使用ChannelHandlerContext
public class MyInboundHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("Connected!");
ctx.fireChannelActive();
}
}
public class MyOutboundHandler extends ChannelOutboundHandlerAdapter {
@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
System.out.println("Closing ..");
ctx.close(promise);
}
}
三、 Channel
api文档上的说明:
A nexus to a network socket or a component which is capable of I/O operations such as read, write, connect, and bind.
A channel provides a user:
* the current state of the channel (e.g. is it open? is it connected?),
* the [configuration parameters](https://netty.io/4.1/api/io/netty/channel/ChannelConfig.html "interface in io.netty.channel") of the channel (e.g. receive buffer size),
* the I/O operations that the channel supports (e.g. read, write, connect, and bind), and
* the [`ChannelPipeline`](https://netty.io/4.1/api/io/netty/channel/ChannelPipeline.html "interface in io.netty.channel") which handles all I/O events and requests associated with the channel.
channel是netty在socket上的一种封装和抽象。
四、ChannelPipeline
文档说明:
A list of ChannelHandlers which handles or intercepts inbound events and outbound operations of a Channel.
ChannelPipeline implements an advanced form of the Intercepting Filter pattern to give a user full control over
how an event is handled and how the ChannelHandlers in a pipeline interact with each other.
首先,ChannelPipeline的模式是:拦截器链模式。
即: 每一个Channel都有一个ChannelHandler链,而这个ChannelHandler链就是ChannelPipeline。 这个链负责处理该channel的所有入站事件和出站操作。
ChannelPipeline何时被创建?
Each channel has its own pipeline and it is created automatically when a new channel is created.即:ChannelPipeline随着channel的创建而创建。
五、 ChannelHandler
ChannelHandler的作用是:
1、处理IO事件
2、拦截IO操作
3、把IO事件、IO操作传递给当前ChannelPipeline中的下一个Handler.
六、 ChannelContextHandler
官网上有这段说明:
doc:
A ChannelHandler is provided with a ChannelHandlerContext object. A ChannelHandler is supposed to interact with the ChannelPipeline it belongs to via a context object.
Using the context object, the ChannelHandler can pass events upstream or downstream, modify the pipeline dynamically, or store the information (using AttributeKeys) which is specific to the handler.
上面这段话的意思是说:
每个ChannelHandler都有一个ChannelHandlerContext对象, ChannelHandler通过ChannelHandlerContext对象可以完成:
1、和ChannelPipe以及其他ChannelHandler交互。
2、向上或向下传递事件
即:ctx是channelHandler和channelPipeline以及和其他channelHandler交互的媒介。