Practical netty (4) Parent-Child channel relationship: Relationship between channels and Pipelines
- Author: Liu Da-poechant (Zhong Chao)
- Email: zhongchao. USTC # gmail.com (#-> @)
- Blog: blog.csdn.net/poechant
- Weibo: weibo.com/lauginhom
- Date: June 10Th, 2012
(The following section is written after the completion of this article.) We call the parent channel as the parent channel, the Child channel as the subchannel, the channel as the channel, and the channelpipeline as the pipeline. Only one such purpose is to shorten the title in the csdn blog... Of course, the English names are still used in the text below, because the Chinese names mentioned above are what I did when I started the title after writing this article.
1. Core netty concepts 3: parent channel and child Channels
After a serverbootstrap is started, a parent channel is created, which is used to accept connections. Each connection is allocated to a child channel for subsequent processing.
The options of parent channel and child channels are set by serverbootstrap. The configurable key and value types are as follows:
"localAddress"
,InetSocketAddress
"keepAlive"
,boolean
"reuseAddress"
,boolean
"soLinger"
,int
"tcpNoDelay"
,boolean
"receiveBufferSize"
,int
"sendBufferSize"
,int
"trafficClass"
,int
Sample Code:
bootstrap.setOption("reuseAddress", true);bootstrap.setOption("localAddress", new InetSocketAddress(port));bootstrap.setOption("child.tcpNoDelay", true);bootstrap.setOption("child.receiveBufferSize", 1048576);
For more information, see the csdn blog blog.csdn.net/poechant,weibo.com/lauginhom.
2. core concepts of netty 4: Channel and channelpipeline
Let's take a look at the official document of netty:
For each new channel, a new pipeline must be created and attached to thechannel. Once attached, the coupling between the channel and the pipelineis permanent; the channel cannot attach another pipeline to it nor detachthe current pipeline from it.
Each channel has a corresponding channelpipe and must have one. For a bootstrap, you must specify how channelpipeline is generated:
bootstrap.setPipelineFactory(ChannelPipelineFactory factory);
.ChannelPipelineFactory
IsInterface
, So you needimplements
For example:
bootstrap.setPipelineFactory(new ChannelPipelineFactory() { ChannelPipeline p = Channels.pipeline(); p.addLast("handler", new PoechantProxyHandler());});
This is an implementation method for anonymous classes. You should also understand real-name classes. The official saying tells us that you can only attach one channelpipeline to a channel, and once attach, you cannot detach it. This is why setpipelinefactory is created, it defines the rule for binding channelpipeline to your channel (including what kind of channelpipeline ).
-
For more information, see the csdn blog blog.csdn.net/poechant,weibo.com/lauginhom.
-