<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                攔截器應該可以說是一個很經典的設計模式,它有點類似于過濾器,當某信息從一個地方流向目的地的過程中,可能需要統一對信息進行處理,如果考慮到系統的可擴展性和靈活性通常就會使用攔截器模式,它就像一個個關卡被設置在信息流動的通道中,并且可以按照實際需要添加和減少關卡。Tribes為了在應用層提供對源消息統一處理的渠道引入通道攔截器,用戶在應用層只需要根據自己需要添加攔截器即可,例如,壓縮解壓攔截器、消息輸出輸入統計攔截器、異步消息發送器等等。 攔截器的數據流向示意圖可以參考前面的tribes簡介章節,數據從IO層流向應用層,中間就會經過一個攔截器棧,應用層處理完就會返回一個ack給發送端表示已經接收并處理完畢(消息可靠級別為SYNC_ACK),下面嘗試用最簡單一些代碼和偽代碼說明tribes的攔截器實現,旨在領會攔截器如何設計而并非具體的實現。最終實現的功能如圖所示,最底層的協調者ChannelCoordinator永遠作為第一個加入攔截器棧的攔截器,往上則是按照添加順序排列,且每個攔截器的previous、next分別指向前一個攔截器和下一個攔截器。 ?![](https://box.kancloud.cn/2016-01-15_5698bd8e2e5f8.jpg) ① 定義攔截器接口 ~~~ public?interface?ChannelInterceptor{ ????public?void?setNext(ChannelInterceptor?next)?; ????public?ChannelInterceptor?getPrevious(); ????public?void?sendMessage(ChannelMessage?msg); ????public?void?messageReceived(ChannelMessage?msg); } ~~~ ② 定義一個基礎攔截器,提供一些公共的操作,由于攔截器執行完后要觸發下個攔截器,所以把觸發工作統一抽離到基礎類里面完成,當然里面必須包含前一個和后一個攔截器的引用。 ~~~ public?class?ChannelInterceptorBase?implements?ChannelInterceptor?{ ????private?ChannelInterceptor?next; ????private?ChannelInterceptor?previous; ????public?ChannelInterceptorBase()?{ ????} ????public?final?void?setNext(ChannelInterceptor?next)?{ ????????this.next?=?next; ????} ????public?final?ChannelInterceptor?getNext()?{ ????????return?next; ????} ????public?final?void?setPrevious(ChannelInterceptor?previous)?{ ????????this.previous?=?previous; ????} ????public?final?ChannelInterceptor?getPrevious()?{ ????????return?previous; ????} ????public?void?sendMessage(ChannelMessage?msg)?{ ????????if?(getNext()?!=?null)?getNext().sendMessage(msg,); ????} ????public?void?messageReceived(ChannelMessage?msg)?{ ????????if?(getPrevious()?!=?null)?getPrevious().messageReceived(msg); ????} } ~~~ ③ 壓縮解壓攔截器,此攔截器負責按一定算法壓縮和解壓處理。 ~~~ public?class?GzipInterceptor?extends?ChannelInterceptorBase?{ ????public?void?sendMessage(ChannelMessage?msg){ ????????????compress?the?msg; ????????????getNext().sendMessage(msg); ????} ????public?void?messageReceived(ChannelMessage?msg)?{ ????????????decompress?the?msg; ????????????getPrevious().messageReceived(msg); ????} } ~~~ ④ 最底層的協調器,直接與網絡IO做交互 ~~~ public?class?ChannelCoordinator?extends?ChannelInterceptorBase{ ????public?ChannelCoordinator()?{ ????} ????public?void?sendMessage(ChannelMessage?msg)?throws?ChannelException?{ ????????Network?IO?Send ????} public?void?messageReceived(ChannelMessage?msg)?{ ????Network?IO?READ ????????super.messageReceived(msg); ????} } ~~~ ⑤ 測試類 ~~~ public?class?Test{ public?void?main(String[]?args){ ChannelCoordinator?coordinator?=?new?ChannelCoordinator(); GzipInterceptor?gzipInterceptor?=?new?GzipInterceptor(); coordinator.setNext(null); coordinator.setPrevious(gzipInterceptor); gzipInterceptor.setPrevious(null); gzipInterceptor?.setNext(coordinator); gzipInterceptor.sendMessage(msg); coordinator.messageReceived(msg); }? } ~~~ ????Tribes的攔截器整體設計就如上面,整個攔截器的執行順序如下,當執行寫操作時,數據流向GzipInterceptor?->?ChannelCoordinator?->?Network?IO;當執行讀操作時,數據流向則為Network?IO?->?ChannelCoordinator?->?GzipInterceptor。理解了整個設計原理后對于tribes的整體把握將會更加深入。 對java有興趣的朋友可以交個朋友 ![](https://box.kancloud.cn/2016-01-15_5698bd8d22882.jpg)
                  <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>

                              哎呀哎呀视频在线观看