Practical Netty (4) 父子频道关系,频道与管道的关系

news/2024/7/3 11:14:29

Practical Netty (4) 父子频道关系,频道与管道的关系

  • 作者:柳大·Poechant(钟超)
  • 邮箱:zhongchao.ustc#gmail.com(# -> @)
  • 博客:Blog.CSDN.net/Poechant
  • 微博:weibo.com/lauginhom
  • 日期:June 10th, 2012

(下面这段话是完成本文后写的)我姑且将 Parent channel 称为父频道,Child Channel 称为子频道,Channel 就是频道了,而 ChannelPipeline 则称为管道。这样的目的只有一个,就是能在 CSDN 博客里把标题弄短一点。。。当然了,下文中仍然用了英文名称,因为上面这几个中文名称是我写完本文后回头起标题的时候,为了缩短才搞的,哈哈。

1. Netty 核心概念之三:Parent Channel 与 Child Channels

一个 ServerBootstrap 启动后,会创建一个 parent channel,这个 parent channel 用于接受 connections,每个 connection 会被分配到一个 child channel 上,用于后续的处理。

Parent channel 和 child channels 的 options 是由 ServerBootstrap 来设置的。可设置的 key 和 value type 如下:

  • "localAddress"InetSocketAddress
  • "keepAlive"boolean
  • "reuseAddress"boolean
  • "soLinger"int
  • "tcpNoDelay"boolean
  • "receiveBufferSize"int
  • "sendBufferSize"int
  • "trafficClass"int

一段示例代码:

bootstrap.setOption("reuseAddress", true);
bootstrap.setOption("localAddress", new InetSocketAddress(port));
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.receiveBufferSize", 1048576);

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant,微博:weibo.com/lauginhom

2. Netty 核心概念之四:Channel 与 ChannelPipeline

先看 Netty 官方文档的一段话:

For each new channel, a new pipeline must be created and attached to the
channel. Once attached, the coupling between the channel and the pipeline
is permanent; the channel cannot attach another pipeline to it nor detach
the current pipeline from it.

每个 Channel 都有对应的一个 ChannelPipe,而且必须有。对于一个 Bootstrap,你需要指定它的 ChannelPipeline 如何产生,这通过:

bootstrap.setPipelineFactory(ChannelPipelineFactory factory);

来实现。ChannelPipelineFactory是一个Interface,所以你需要implements它,举例如下:

bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    ChannelPipeline p = Channels.pipeline();
    p.addLast("handler", new PoechantProxyHandler());
});

这是一个匿名类的实现方式,实名类你也应该懂的。官方那段话告诉我们,你给一个 Channel 只能 attach 一个 ChannelPipeline,而且一旦 attach 就不能 detach 了,这也就是为什么会有一个 setPipelineFactory 的东东的原因,它来定义给你的 Channel 绑定 ChannelPipeline 的规则(包括什么样的 ChannelPipeline)。

-

转载请注明来自柳大的CSDN博客:Blog.CSDN.net/Poechant,微博:weibo.com/lauginhom


http://www.niftyadmin.cn/n/3959859.html

相关文章

开车啦开车啦

目录 结对结对!任务要求题目介绍界面界面!代码小伙伴介绍结对结对! (软件工程第三次作业)[二人结对|极限编程|复查] 任务要求 1.能够自动生成四则运算练习题2. :可以定制题目数量3 :用户设置最大数(如十以内…

Think in java 读书笔记

第二章:一切皆对象2.1:类 又称为对象的类型!2.2: 本书中,谈到‘类型’都是指class 对象的类型 说的是class ! new class() 是类型实例化出来的对象!2.3&#xff1…

Practical Netty (5) TCP反向代理服务器

Practical Netty (5) TCP反向代理服务器 作者:柳大Poechant(钟超)邮箱:zhongchao.ustc#gmail.com(# -> )博客:Blog.CSDN.net/Poechant 微博:weibo.com/lauginhom 日期&#xff1a…

Practical Netty (6) HTTP Server/Client

Practical Netty (6) HTTP Server/Client 作者:柳大Poechant(钟超)邮箱:zhongchao.ustc#gmail.com(# -> )博客:Blog.CSDN.net/Poechant 微博:weibo.com/lauginhom 日期&#xff1…

51 NOD 1406 and query

我们知道一个数S会对所有它的子集S产生1的贡献,但是我们直接枚举子集是 3^(log2 1000000)的,会炸掉;如果直接把每个有1的位变成0往下推也会凉掉,因为这样会有很多重复的。 但是我们发现 第二种方法其实算的是 有序的路径方案数&am…

java对象的内存计算

我们讨论的是java heap中对象所占内存。 1.基本类型内存占用 类型占用字节数boolean1byte1char2short2int4float4long8double82.对象所占内存由以下部分组成 object header, 8 byte基本类型,见第1节的表格引用类型,都为4 bytepadding&#…

[JavaScript]多文件上传时动态添加及删除文件选择框

多文件上传时,首先要解决的一个问题就是动态去添加或删除文件选择框,原来以为没多么困难的,但是没想到IE居然不支持table.appendChild()的js代码,导致整个前台JS的实现时间比原计划大大增加。不过还好可以借助网络查找需要的资源&…

第四周疑难点

18-20题的编程和解读不清楚? https://blog.csdn.net/a1015553840/article/details/50979640 c编译器有点问题,搞好IDE再回来。 林老师的优秀博客资料: https://blog.csdn.net/red_stone1/article/category/6956972转载于:https://www.cnblogs…