AbstractBootstrap是一個幫助類,通過方法鏈(method chaining)的方式,提供了一個簡單易用的方式來配置Bootstrap,然后啟動一個Channel。在理解Netty源碼中的AbstractBootstrap, ServerBootstrap和Bootstrap之前,應該先了解一下什么是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_NODELAY,AIO_READ_TIMEOUT |
Map<AttributeKey<?>, Object> attrs | 指定所創建Channel的屬性 |
ChannelHandler handler | 用于處理各類請求 |
AbstractBootstrap抽象類的接口

AbstractBootstrap抽象類UML圖
ServerBootstrap和Bootstrap
在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;