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
相关推荐
netty4与spring集成;客户端服务端代码实现。netty4与spring集成。
netty+4G DTU
netty4 sources 源码
netty4-demosnetty4-demosnetty4-demosnetty4-demosnetty4-demos
netty4 中文学习资料
ChannelHandlerAdapter 4.X版本和5.X版本的差别很大。ChannelRead是属于5.X版本的4.X版本没有这个方法,所以如果要用ChannelRead。可以更换5.X版本的Netty。
这本书是基于netty4的, 大家看好版本可以去下载。。。
netty 4.1中文.CHM,chm格式,中文api,开发netty的中文工具书
netty4 所有jar包,需要的可以下载
赠送jar包:transport-netty4-client-5.5.1.jar; 赠送原API文档:transport-netty4-client-5.5.1-javadoc.jar; 赠送源代码:transport-netty4-client-5.5.1-sources.jar; 赠送Maven依赖信息文件:transport-netty...
Netty管道测试代码
通过netty4实现的简单的服务端与客户端的源代码,采用IntelliJ IDEA 2018.1.3开发工具,该例子可以帮助你入门netty框架。
赠送jar包:transport-netty4-client-5.5.1.jar; 赠送原API文档:transport-netty4-client-5.5.1-javadoc.jar; 赠送源代码:transport-netty4-client-5.5.1-sources.jar; 赠送Maven依赖信息文件:transport-netty...
赠送jar包:transport-netty4-client-6.3.0.jar; 赠送原API文档:transport-netty4-client-6.3.0-javadoc.jar; 赠送源代码:transport-netty4-client-6.3.0-sources.jar; 赠送Maven依赖信息文件:transport-netty...
Netty4.x源码分析详解
Netty4.x的demo.版本4.0.28,配置tomcat7,jdk7.使用方法:导入的eclipse中,运行HelloServer.java,再运行HelloClient.java 这样客户端和服务器就可以通信了。希望对初次接触Netty的同学有所帮助!
使用Netty4实现多线程的消息分发,这是一个基于netty4做的一个异步通信模型。
netty 4 demo例子, 出自netty 权威指南, 代码结构分层清晰
从源码和网上资料,总结了Mina2与Netty4的区别,主要是分析线程模型的Buffer,还提到了Netty4中引入的Google的优秀的序列化工具ProtoBuf
netty4 maven仓库2 上传给自己下载用的 请让我上传了