Netty Channel与ChannelPipeline的关系

1.Channel:

  • ServerSocketChannel
  • SocketChannel

2.pipeline:

 一个pipeline当中包含了多个ChannelHandler,而且是有顺序的

3.ChannelerHandler

  • HttpRequestDecode:解码请求
  • HttpResponseEncode:编码返回结果

在Netty中每个Channel都有仅有一个ChannelPipeline与之对应,它们的组成关系如下:
Netty Channel与ChannelPipeline的关系_第1张图片

通过商入我们可以看到,一个Channel包含了一个ChannelPipeline,而ChannelPipeline中又维护了一个由ChannelHandlerCOntext组成的双向链表,这个链表的头是HeadContext,链表的尾是TailContext,并且么个ChannelHandlerContext中又关联着一个CHannelHandler;

程序中如何初始化pipeline:

bootstrap.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());
ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));
ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());
ch.pipeline().addLast("http-server", new HttpServerHandler());
}
});

Netty Channel与ChannelPipeline的关系_第2张图片

你可能感兴趣的:(Netty)