<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                有時你可能需要引導客戶端 Channel 從另一個 Channel。這可能發生,如果您正在編寫一個代理或從其他系統需要檢索數據。后一種情況是常見的,因為許多 Netty 的應用程序集成現有系統,例如 web 服務或數據庫。 你當然可以創建一個新的 Bootstrap 并使用它如9.2.1節所述,這個解決方案不一定有效。至少,你需要創建另一個 EventLoop 給新客戶端 Channel 的,并且 Channel 將會需要在不同的 Thread 間進行上下文切換。 幸運的是,由于 EventLoop 繼承自 EventLoopGroup ,您可以通過傳遞 接收到的 Channel 的 EventLoop 到 Bootstrap 的 group() 方法。這允許客戶端 Channel 來操作 相同的 EventLoop,這樣就能消除了額外的線程創建和所有相關的上下文切換的開銷。 *為什么共享 EventLoop 呢?* *當你分享一個 EventLoop ,你保證所有 Channel 分配給 EventLoop 將使用相同的線程,消除上下文切換和相關的開銷。(請記住,一個EventLoop分配給一個線程執行操作。)* 共享一個 EventLoop 描述如下: [![](https://box.kancloud.cn/2015-08-19_55d477fa2afab.jpg)](https://github.com/waylau/essential-netty-in-action/blob/master/images/Figure%209.4%20EventLoop%20shared%20between%20channels%20with%20ServerBootstrap%20and%20Bootstrap.jpg) 1. 當 bind() 調用時,ServerBootstrap 創建一個新的ServerChannel 。 當綁定成功后,這個管道就能接收子管道了 2. ServerChannel 接收新連接并且創建子管道來服務它們 3. Channel 用于接收到的連接 4. 管道自己創建了 Bootstrap,用于當 connect() 調用時創建一個新的管道 5. 新管道連接到遠端 6. 在 EventLoop 接收通過 connect() 創建后就在管道間共享 Figure 9.4 EventLoop shared between channels with ServerBootstrap and Bootstrap 實現 EventLoop 共享,包括設置 EventLoop 引導通過Bootstrap.eventLoop() 方法。這是清單9.5所示。 ~~~ ServerBootstrap bootstrap = new ServerBootstrap(); //1 bootstrap.group(new NioEventLoopGroup(), //2 new NioEventLoopGroup()).channel(NioServerSocketChannel.class) //3 .childHandler( //4 new SimpleChannelInboundHandler<ByteBuf>() { ChannelFuture connectFuture; @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { Bootstrap bootstrap = new Bootstrap();//5 bootstrap.channel(NioSocketChannel.class) //6 .handler(new SimpleChannelInboundHandler<ByteBuf>() { //7 @Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf in) throws Exception { System.out.println("Reveived data"); } }); bootstrap.group(ctx.channel().eventLoop()); //8 connectFuture = bootstrap.connect(new InetSocketAddress("www.manning.com", 80)); //9 } @Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { if (connectFuture.isDone()) { // do something with the data //10 } } }); ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080)); //11 future.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { if (channelFuture.isSuccess()) { System.out.println("Server bound"); } else { System.err.println("Bound attempt failed"); channelFuture.cause().printStackTrace(); } } }); ~~~ 1. 創建一個新的 ServerBootstrap 來創建新的 SocketChannel 管道并且綁定他們 2. 指定 EventLoopGroups 從 ServerChannel 和接收到的管道來注冊并獲取 EventLoops 3. 指定 Channel 類來使用 4. 設置處理器用于處理接收到的管道的 I/O 和數據 5. 創建一個新的 Bootstrap 來連接到遠程主機 6. 設置管道類 7. 設置處理器來處理 I/O 8. 使用相同的 EventLoop 作為分配到接收的管道 9. 連接到遠端 10. 連接完成處理業務邏輯 (比如, proxy) 11. 通過配置了的 Bootstrap 來綁定到管道 注意,新的 EventLoop 會創建一個新的 Thread。出于該原因,EventLoop 實例應該盡量重用。或者限制實例的數量來避免耗盡系統資源。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看