2016-02-19 65 views
4

根據該文檔New and noteworthy in 4.0 ServerBootstrap.option()和ServerBootstrap.childOption()之間的差,netty4提供了一種新的自舉的API,並且該文檔給出了下面的代碼示例:是什麼在網狀4.x的

public static void main(String[] args) throws Exception { 
    // Configure the server. 
    EventLoopGroup bossGroup = new NioEventLoopGroup(); 
    EventLoopGroup workerGroup = new NioEventLoopGroup(); 
    try { 
     ServerBootstrap b = new ServerBootstrap(); 
     b.group(bossGroup, workerGroup) 
     .channel(NioServerSocketChannel.class) 
     .option(ChannelOption.SO_BACKLOG, 100) 
     .localAddress(8080) 
     .childOption(ChannelOption.TCP_NODELAY, true) 
     .childHandler(new ChannelInitializer<SocketChannel>() { 
      @Override 
      public void initChannel(SocketChannel ch) throws Exception { 
       ch.pipeline().addLast(handler1, handler2, ...); 
      } 
     }); 

     // Start the server. 
     ChannelFuture f = b.bind().sync(); 

     // Wait until the server socket is closed. 
     f.channel().closeFuture().sync(); 
    } finally { 
     // Shut down all event loops to terminate all threads. 
     bossGroup.shutdownGracefully(); 
     workerGroup.shutdownGracefully(); 

     // Wait until all threads are terminated. 
     bossGroup.terminationFuture().sync(); 
     workerGroup.terminationFuture().sync(); 
    } 
} 

該代碼使用ServerBootStrap.option來設置ChannelOption.SO_BACKLOG,然後使用ServerBootStrap.childOption來設置ChannelOption.TCP_NODELAY

ServerBootStrap.optionServerBootStrap.childOption有什麼區別?我怎麼知道哪個選項應該是option,哪個應該是childOption

回答

4

ServerBootStrap.option和 ServerBootStrap.childOption有什麼區別?

我們設置使用ServerBootStrap.option適用於新創建的ServerChannel的ChannelConfig,即,它偵聽並接受客戶端連接的服務器套接字的參數。這些選項將在調用bind()或connect()方法時在服務器通道上設置。這個頻道是每個服務器一個。

ServerBootStrap.childOption適用於通道的channelConfig,它在serverChannel接受客戶端連接後創建。此通道是每個客戶端(或每個客戶端套接字)。

因此ServerBootStrap.option參數適用於監聽連接的服務器套接字(服務器通道),而ServerBootStrap.childOption參數適用於一旦服務器套接字接受連接後創建的套接字。

同樣可以VS在ServerBootstrapchildHandler方法被擴展到attr VS childAttrhandler

我怎麼能知道哪個選項應該是一個選項,哪個應該是 一個childOption?

支持哪些ChannelOptions取決於我們使用的通道類型。 您可以參考您正在使用的ChannelConfig的API文檔。 http://netty.io/4.0/api/io/netty/channel/ChannelConfig.html及其子類。您應該爲每個ChannelConfig找到可用選項部分。

相關問題