clientConnectionFuture=tcpNioTransport.connect("grizzly.j" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Grizzly NIO框架 理論篇 【2】

系統(tǒng) 1906 0

Transports and Connections

這兩個(gè)東西,是灰熊框架的核心結(jié)構(gòu)~Transports工具包含有TCP或UDP的協(xié)議,合并各種組件資源(Thread-Pool 線程池、Memory Manager 內(nèi)存管理器等等。
""
從結(jié)構(gòu)來說,沒什么東西,主要還是使用上
1、使用Future模式建立Connection
  1. Future < Connection > clientConnectionFuture = tcpNioTransport . connect ( "grizzly.java.net" , 80 );
  2. Connection clientConnection = clientConnectionFuture . get ();
?
2、可以針對(duì)Connection添加CloseHandler等事件處理
  1. Connection conn ;
  2. conn . addCloseListener ( new CloseListener < Closeable , ICloseType >() {
  3. @Override
  4. public void onClosed ( Closeable closeable , ICloseType type )
  5. throws IOException {
  6. // TODO Auto-generated method stub
  7. }
  8. });

FilterChains and Filters

過濾器給予了Grizzly無限擴(kuò)展性。FilterChains是Filter的過程封裝,可以對(duì)Filter的逐步處理。
比如要擴(kuò)展成http、Filter組合如下:
""
比如要給把http修改成https,那么只需要增加SSLFilter:
?
""
其中,Transport Filter是所有處理的基礎(chǔ),用于和Grizzly的Selector交互;
另外我認(rèn)需要認(rèn)識(shí)下BaseFilter,所有Filter都繼承于它:
  1. /**
  2. * 讀流程,讀取網(wǎng)絡(luò)數(shù)據(jù),或上一個(gè)Filter read之后的message
  3. */
  4. public NextAction handleRead ( FilterChainContext ctx ) throws IOException ;
  5. /**
  6. * 寫流程,寫如網(wǎng)絡(luò)數(shù)據(jù),或者處理上一個(gè)Filter write之后的message
  7. */
  8. public NextAction handleWrite ( FilterChainContext ctx ) throws IOException ;
  9. /**
  10. * 新加入的連接
  11. */
  12. public NextAction handleConnect ( FilterChainContext ctx ) throws IOException ;
  13. /**
  14. * 同handleConnect
  15. */
  16. public NextAction handleAccept ( FilterChainContext ctx ) throws IOException ;
  17. /**
  18. * 連接斷開之后調(diào)用
  19. */
  20. public NextAction handleClose ( FilterChainContext ctx ) throws IOException ;
兩個(gè)Filter之間如何銜接起來,我們需要了解NextAction是怎么工作,并有哪些類型:

StopAction

如果要停止Filter當(dāng)前工作流程,就直接返回
return ctx.getStopAction();
如果需要中斷工作,并把當(dāng)前沒有處理的數(shù)據(jù)流放到下一次調(diào)用,可以加入?yún)?shù):

return ctx.getStopAction(incompleteChunk);

InvokeAction

如果是繼續(xù)下一次Filter流程,就返回InvokeAction

return ctx.getInvokeAction();

如果數(shù)據(jù)流里面有粘包(兩個(gè)數(shù)據(jù)包一起發(fā)來,我們需要一個(gè)個(gè)處理),同樣可以添加參數(shù):

return ctx.getInvokeAction(incompleteChunk);

RerunFilterAction

return ctx.getRerunFilterAction()

這個(gè)就是當(dāng)前Filter的action多執(zhí)行一次。

SuspendAction

return ctx.getSuspendAction();

這個(gè)是暫停。。。。中斷線程,并且通過另外一個(gè)線程調(diào)用

ctx.resume() :

ForkAction (was SuspendStopAction)

return ctx.getForkAction();

和上一個(gè)類似。





Grizzly NIO框架 理論篇 【2】


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 台江县| 青龙| 汽车| 阜宁县| 汉源县| 喜德县| 高雄县| 紫云| 平果县| 徐汇区| 台州市| 北流市| 临泉县| 依安县| 申扎县| 鄂尔多斯市| 盐山县| 南岸区| 民和| 台前县| 合肥市| 普兰县| 浮山县| 惠安县| 永平县| 桐乡市| 来安县| 衡阳县| 哈巴河县| 仪征市| 九江县| 肥东县| 海伦市| 灵丘县| 林甸县| 鄂托克旗| 班玛县| 温州市| 上虞市| 屏东市| 光山县|