csusky

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          2008年12月12日 #

          異步IO的關(guān)閉事件

          JAVA SOCKET只定義了四種事件

          public static final int OP_READ = 1 << 0;
          public static final int OP_WRITE = 1 << 2;
          public static final int OP_CONNECT = 1 << 3;
          public static final int OP_ACCEPT = 1 << 4;

          是沒有關(guān)閉事件的,我們?cè)趺磁袛嘁粋€(gè)連接是否關(guān)閉呢?
          如果你的selector注冊(cè)了一個(gè)OP_READ事件,那么在連接關(guān)閉的時(shí)候?qū)?huì)產(chǎn)生一個(gè)OP_READ事件
          也就是說本來阻塞的selector此時(shí)將會(huì)被喚醒,但是如果試圖在此事件的通道中讀取數(shù)據(jù)將會(huì)返回-1
          如下:

          Set<SelectionKey> readyKeys = selector.selectedKeys();

          = readyKeys.iterator()

          SelectionKey key 
          = (SelectionKey)i.next();

          if (operation == SelectionKey.OP_READ &&
                                   key.isReadable())
                          
          {
                              ReadableByteChannel incomingChannel 
          = (ReadableByteChannel)key.channel(); 
          //此時(shí)將會(huì)得到-1,表明該鏈接已關(guān)閉
          int n = incomingChannel.read(readBuffer);
          }
          此時(shí)我們需要取消該KEY 如下:
          if (n == -1)
                      
          {
                          key.cancel();
                            //關(guān)閉輸入輸出 
                            sc.socket().shutdownOutput();
                            sc.socket().shutdownInput();
                             //關(guān)閉SOCKET
                             sc.socket().close();
                            //關(guān)閉通道
                             incomingChannel.close();
                      }

          posted @ 2009-11-10 22:28 曉宇 閱讀(425) | 評(píng)論 (1)編輯 收藏

          ExecutorFilter

          1 . 用Executors構(gòu)造一個(gè)新的線程池
          ExecutorService executor = Executors.newCachedThreadPool();

          方法 newCachedThreadPool();
          創(chuàng)建一個(gè)可根據(jù)需要?jiǎng)?chuàng)建新線程的線程池,但是在以前構(gòu)造的線程可用時(shí)將重用它們,并在需要時(shí)使用提供的 ThreadFactory 創(chuàng)建新線程。
          2. 用構(gòu)造的線程池創(chuàng)建ExecutorFilter
          ExecutorFilter es= new ExecutorFilter(executor));

          在ExecutorFilter內(nèi)部:
          只需要將相應(yīng)的事件分發(fā)到到線程池的相應(yīng)線程即可,但是SessionCreated事件只能在主線程中,不能分發(fā)
          觸發(fā)方法
          1 .
          首先構(gòu)造一個(gè)IoFilterEvent,這個(gè)IoFilterEvent包含1、事件的類型,2、下一個(gè)過濾器
          然后觸發(fā)該時(shí)間的處理方法。
           if (eventTypes.contains(IoEventType.SESSION_OPENED)) {
                      fireEvent(
          new IoFilterEvent(nextFilter, IoEventType.SESSION_OPENED,
                              session, 
          null));
                  }

          2 .
          從線程池中取出一個(gè)線程執(zhí)行事件處理
          protected void fireEvent(IoFilterEvent event) {
                  getExecutor().execute(event);
              }


          在構(gòu)造ExecutorFilter 時(shí)如果沒有傳入IoEventType則默認(rèn)只對(duì)如下幾種幾件感興趣
          EXCEPTION_CAUGHT
          MESSAGE_RECEIVED
          MESSAGE_SENT
          SESSION_CLOSED
          SESSION_IDLE
          SESSION_OPENED
          當(dāng)然還需要覆蓋相應(yīng)的事件處理方法 如上所示

          posted @ 2008-12-12 11:33 曉宇 閱讀(1560) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 临沧市| 伊宁县| 天门市| 衡水市| 大同市| 吉木萨尔县| 澎湖县| 武夷山市| 河西区| 永泰县| 五华县| 澄城县| 云梦县| 巩义市| 松江区| 五大连池市| 镇沅| 壤塘县| 马鞍山市| 灌云县| 营山县| 龙岩市| 马关县| 四子王旗| 平果县| 柏乡县| 当雄县| 莎车县| 金湖县| 登封市| 昭觉县| 共和县| 辽阳县| 磐石市| 茌平县| 光泽县| 定远县| 阿拉善盟| 阳城县| 沙雅县| 九寨沟县|