<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Chan Chen Coding...

    Netty 4.0 源碼分析(七):AbstractBootstrap抽象類

    AbstractBootstrap是一個幫助類,通過方法鏈(method chaining)的方式,提供了一個簡單易用的方式來配置Bootstrap,然后啟動一個Channel。在理解Netty源碼中的AbstractBootstrap, ServerBootstrapBootstrap之前,應該先了解一下什么是method chaining。


    Wiki
    上面對于method chaining的定義如下:

    Method chaining, is a common technique for invoking multiple method calls in object-oriented programming languages. Each method returns an object (possibly the current object itself), allowing the calls to be chained together in a single statement.

    在面向對象的編程語言中,method chaining是一種用于調用多個方法的常用技術。每個方法都會返回一個對象(可能是當前對象本身),這樣做的好處就是,通過一條語句,就可以講所有的方法調用鏈接在一起。 


    定義有一些抽象,那么可以通過實際的例子來進行理解和消化。

    假設有一個StringBuffer的類,類中有一個append()方法

    public class StringBuffer {
      public void append(String str) {
        //  useful code 
      }
    }

     

    這個類有缺點就是說,在向StringBuffer對象追加新的字符串的時候,需要在多條語句中,不斷的調用append()方法。

    StringBuffer sb = new StringBuffer();
    sb.append("Hello ");
    sb.append(name);
    sb.append("! Welcome!");

    method chaining的實現,就可以避免這個缺陷。

    public class StringBuffer {
      public StringBuffer append(String str) {
        //  useful code 
        return this;
      }
    }

    現在我們就可以再一條語句中實現第一個例子的功能了。

    StringBuffer sb = new StringBuffer();
    sb.append("Hello ").append(name).append("! Welcome!");


    AbstractBootstrap
    抽象類 (部分代碼)

    package io.netty.bootstrap;
     
    public abstract class AbstractBootstrap<B extends AbstractBootstrap<?>> {
        private EventLoopGroup group;
        private ChannelFactory factory;
        private SocketAddress localAddress;
        private final Map<ChannelOption<?>, Object> options = new LinkedHashMap<ChannelOption<?>, Object>();
        private final Map<AttributeKey<?>, Object> attrs = new LinkedHashMap<AttributeKey<?>, Object>();
    private ChannelHandler handler;
     
        @SuppressWarnings("unchecked")
        public B group(EventLoopGroup group) {
            if (group == null) {
                throw new NullPointerException("group");
            }
            if (this.group != null) {
                throw new IllegalStateException("group set already");
            }
            this.group = group;
            return (B) this;
        }
        public B channel(Class<? extends Channel> channelClass) {
            if (channelClass == null) {
                throw new NullPointerException("channelClass");
            }
            return channelFactory(new BootstrapChannelFactory(channelClass));
        }
        @SuppressWarnings("unchecked")
        public B channelFactory(ChannelFactory factory) {
            if (factory == null) {
                throw new NullPointerException("factory");
            }
            if (this.factory != null) {
                throw new IllegalStateException("factory set already");
            }
            this.factory = factory;
            return (B) this;
        }
        @SuppressWarnings("unchecked")
        public B localAddress(SocketAddress localAddress) {
            this.localAddress = localAddress;
            return (B) this;
        }
        @SuppressWarnings("unchecked")
        public <T> B option(ChannelOption<T> option, T value) {
            if (option == null) {
                throw new NullPointerException("option");
            }
            if (value == null) {
                options.remove(option);
            } else {
                options.put(option, value);
            }
            return (B) this;
        }
        public <T> B attr(AttributeKey<T> key, T value) {
            if (key == null) {
                throw new NullPointerException("key");
            }
            if (value == null) {
                attrs.remove(key);
            } else {
                attrs.put(key, value);
            }
            return (B) this;
        }
        @SuppressWarnings("unchecked")
        public B handler(ChannelHandler handler) {
            if (handler == null) {
                throw new NullPointerException("handler");
            }
            this.handler = handler;
            return (B) this;
        }
    }

     

     

    AbstractBootstrap聲明了六個私有的成員變量,EventLoopGroup對象,ChannelFacotry對象,SockteAddress對象,Map<ChannelOption<?>,Object>對象,Map<Attribute<?>,Object>對象,ChannelHandler對象。并且有相對應的方法,來設置這些對象,如

    public B group(EventLoopGroup group) {
            if (group == null) {
                throw new NullPointerException("group");
            }
            if (this.group != null) {
                throw new IllegalStateException("group set already");
            }
            this.group = group;
            return (B) this;
    }

     

    通過傳進來的group對象,然后將AbstractBootstrap中的group指向傳進來的group。最終返回的還是AbstractBootstrap這個當前對象。

     

    六個對象的作用

    EventLoopGroup group

    用于處理將要被創建的所有event

    ChannelFactory factory

    建立一個工廠,用于以后的Channel創建

    SocketAddress localAddress;

    用于綁定本地的網絡地址

    Map<ChannelOption<?>, Object> options

    指定所創建Channel的選項,如TCP_NODELAYAIO_READ_TIMEOUT

    Map<AttributeKey<?>, Object> attrs

    指定所創建Channel的屬性

    ChannelHandler handler

    用于處理各類請求

     

    AbstractBootstrap抽象類的接口

     


    AbstractBootstrap抽象類UML

     

    ServerBootstrapBootstrap

    Netty 4.0中,ServerBootstrap用于為服務器啟動ServerChannel,Bootstrap用于為客服端啟動Channel。這兩個類是AbstractBootstrap的具體實現。

    備注:因為筆者開始寫Netty源碼分析的時候,Netty 4.0還是處于Alpha階段,之后的API可能還會有改動,筆者將會及時更改。使用開源已經有好幾年的時間了,一直沒有時間和精力來具體研究某個開源項目的具體實現,這次是第一次寫開源項目的源碼分析,如果文中有錯誤的地方,歡迎讀者可以留言指出。對于轉載的讀者,請注明文章的出處。
    希望和廣大的開發者/開源愛好者進行交流,歡迎大家的留言和討論。

     



    -----------------------------------------------------
    Silence, the way to avoid many problems;
    Smile, the way to solve many problems;

    posted on 2012-11-26 10:57 Chan Chen 閱讀(2180) 評論(0)  編輯  收藏 所屬分類: Netty

    主站蜘蛛池模板: 久久午夜免费视频| 99re6在线视频精品免费下载| 在线看片韩国免费人成视频| 鲁丝片一区二区三区免费 | 免费v片视频在线观看视频| 亚洲sss综合天堂久久久| 欧洲黑大粗无码免费| 亚洲色精品VR一区区三区| 毛片a级三毛片免费播放| 一本久久a久久精品亚洲| 中文字幕版免费电影网站| 国产va免费精品观看精品| 亚洲AV一二三区成人影片| 久久免费香蕉视频| 亚洲av无码一区二区乱子伦as | 国产在亚洲线视频观看| 亚洲av无码乱码在线观看野外| 亚洲第一综合天堂另类专| 午夜亚洲福利在线老司机| 久久久久久毛片免费看| 色婷婷六月亚洲婷婷丁香| 天天影视色香欲综合免费| 中日韩亚洲人成无码网站| 免费大黄网站在线观| 青青操免费在线视频| 亚洲日韩乱码中文无码蜜桃臀| 在线观看人成视频免费| 一级黄色免费网站| 亚洲色图在线播放| 麻豆国产精品入口免费观看| av午夜福利一片免费看久久| 亚洲av无码专区在线播放| 在线观看免费人成视频色9| 黄色毛片免费观看| 亚洲成人精品久久| 国产精品高清全国免费观看| 99免费精品视频| 国产人成亚洲第一网站在线播放| 亚洲av手机在线观看| 99久久精品免费视频| 337P日本欧洲亚洲大胆精品|