一 簡介 java
1 Only for connection oriented transports bootstrap
該類用於建立一個server端的通道,用於接收 例如 tcp 和 本地的鏈接,不能用於接收udp鏈接。 tcp
2 Parent channel and its children spa
parent channel 是一個用來接收鏈接的通道。能夠用 bootstrap 的channelFactory的bind方法建立 .net
一旦成功 bind,parent channel開始接收鏈接,而且接收的鏈接成爲這個parent channel的child channel; 指針
3 Configuring channels code
setOption方法能夠用來給parent channel和childparent設置配置信息。 orm
爲了配置 child channel ,須要在配置文件的名字前面加上 child. server
例如 事件
{@link ServerBootstrap} b = ...; * * // Options for a parent channel * b.setOption("localAddress", new {@link InetSocketAddress}(8080)); * b.setOption("reuseAddress", true); * * // Options for its children * b.setOption("child.tcpNoDelay", true); * b.setOption("child.receiveBufferSize", 1048576);
能夠經過 channelConfig和他的子類查看配置的詳細信息
4 Configuring a parent channel pipeline
每個channel 都有他本身的 channelPipeline ,能夠經過兩種方式對其進行配置
a 推薦的方式是 經過 setPipelineFactory 方法設置一個 ChannelPipelineFactory
{@link ServerBootstrap} b = ...; * b.setPipelineFactory(new MyPipelineFactory()); * * public class MyPipelineFactory implements {@link ChannelPipelineFactory} { * public {@link ChannelPipeline} getPipeline() throws Exception { * // Create and configure a new pipeline for a new channel. * {@link ChannelPipeline} p = {@link Channels}.pipeline(); * p.addLast("encoder", new EncodingHandler()); * p.addLast("decoder", new DecodingHandler()); * p.addLast("logic", new LogicHandler()); * return p; * } * }
b.另一種方式,在一種特定的解決方法下起做用,用於默認的pipeline 而且讓 bootstrap 淺copy默認的pipeline 爲了每個新的channel
* {@link ServerBootstrap} b = ...; * {@link ChannelPipeline} p = b.getPipeline(); * * // Add handlers to the default pipeline. * p.addLast("encoder", new EncodingHandler()); * p.addLast("decoder", new DecodingHandler()); * p.addLast("logic", new LogicHandler());
這裏要注意 淺拷貝 表示 添加channelHandler 不是其clone 而僅僅是 指針加到新的 pipeline。
所以 你不能用這個方式若是你計劃打開多於一個的channel 或者運行用來接收鏈接來建立字channel的server模式
5, Applying different settings for different {@link Channel}s
serverBootstrap 僅僅是一個輔助類。他既不分配也無論理資源。
管理資源的是 ChannelFactory 類,他實現了在 ServerBootstrap中定義的實現,
所以,能夠建立不少ServerBootstrap 實例 只要 你使用同一個ChannelFactory實例 去 爲不一樣的channel應用不一樣的設置。
二 屬性
volatile ChannelHandler parentHandler
三 方法
1 ServerBootstrap()
2.ServerBootstrap(ChannelFactory channelFactory)
3.void setFactory(ChannelFactory factory)
4.ChannelHandler getParentHandler()
返回 一個可選的 channelHandler ,他會攔截 一個 最近的接受客戶端鏈接的 server端channel的事件
5.void setParentHandler(ChannelHandler parentHandler)
參考上面
6.Channel bind()
建立一個新的channel ,用於綁定本地地址
相似於
* {@link ServerBootstrap} b = ...; * b.bind(b.getOption("localAddress"));
7 Channel bind(final SocketAddress localAddress)
建立一個新的channel ,綁定到指定的本地地址
流程:
7.1 若是 傳入的參數 localAddress爲空 則拋出異常 NullPointerException("localAddress")
7.2 構建一個 LinkedBlockingQueue<ChannelFuture> futureQueue實例
7.3 經過localAddress 和 LinkedBlockingQueue<ChannelFuture> 構建一個 Binder 實例
7.4 獲取parentHandler
7.5 經過 Channels.pipeline() 構建一個新的 DefaultChannelPipeline 實例 做爲 bossPipeline
7.6 bossPipeline.addLast("binder", binder);
7.7 若是 parentHandler不爲空 則 bossPipeline.addLast("userHandler", parentHandler)
7.8 經過 channelFactory.newChannel(bossPipeline) 返回一個新的channel實例
7.9 循環獲取 futureQueue的值 ChannelFuture 若是 返回成功則 返回channel,不然拋出異常