ServerBootstrap
為什么80%的碼農都做不了架構師?>>> ??
一 簡介?
? ? 1 Only for connection oriented transports
? ? ? 該類用于創建一個server端的通道,用于接收 例如 tcp 和 本地的連接,不能用于接收udp連接。
? ? 2 Parent channel and its children
? ? ? ?parent channel 是一個用來接收連接的通道??梢杂?bootstrap 的channelFactory的bind方法創建
? ? ? ?一旦成功 bind,parent channel開始接收連接,并且接收的連接成為這個parent channel的child channel;
? ? 3 Configuring channels
? ? ? setOption方法可以用來給parent channel和childparent設置配置信息。
? ? ? 為了配置 child channel ,需要在配置文件的名字前面加上 child.
? ? ?例如? ??
{@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,否則拋出異常
? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ??
? ?? ? ? ? ? ??
? ? ? ?
轉載于:https://my.oschina.net/dyaod/blog/483972
總結
以上是生活随笔為你收集整理的ServerBootstrap的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编程之美-快速费波拉契数列
- 下一篇: 二叉链表的建立和遍历 完整的前,中,后和