Mina2.0架構源碼剖析(五)

來源:互聯網
上載者:User

 前面介紹過IoSessionRecycler是負責回收不再使用的會話的介面,ExpiringSessionRecycler是其一個實作類別,用於回收逾時失效的會話。

private ExpiringMap<Object, IoSession> sessionMap;//待處理的會話集

private ExpiringMap<Object, IoSession>.Expirer mapExpirer;//負責具體的回收工作

sessionMap的鍵是由本地地址和遠端地址共同組成的,值是這兩個地址對應的會話。

Expirer類實現了Runnable介面,這個線程負責監控ExpiringMap,並把ExpiringMap中超過閥值的元素從ExpiringMap中移除。這個線程調用了setDaemon(true),因此是作為守護線程在後台運行。具體的處理過程如下:

Java代碼
  1. private
     
    void
     processExpires()   
  2.      {  
  3.          long
     timeNow = System.currentTimeMillis();
    //目前時間
      
  4.          for
     (ExpiringObject o : delegate.values())   
  5.          {  
  6.              if
     (timeToLiveMillis <= 
    0
    )   
  7.              {  
  8.                  continue
    ;  
  9.              }  
  10.              long
     timeIdle = timeNow - o.getLastAccessTime();
    //時間差
      
  11.              if
     (timeIdle >= timeToLiveMillis)   
  12.              {//逾時
      
  13.                  delegate.remove(o.getKey());  
  14.                  for
     (ExpirationListener<V> listener : expirationListeners)   
  15.                  {//呼叫監聽者
      
  16.                      listener.expired(o.getValue());  
  17.                  }  
  18.              }  
  19.          }  
  20.      }  
   private void processExpires()         {            long timeNow = System.currentTimeMillis();//目前時間            for (ExpiringObject o : delegate.values())             {                if (timeToLiveMillis <= 0)                 {                    continue;                }                long timeIdle = timeNow - o.getLastAccessTime();//時間差                if (timeIdle >= timeToLiveMillis)                 {//逾時                    delegate.remove(o.getKey());                    for (ExpirationListener<V> listener : expirationListeners)                     {//呼叫監聽者                        listener.expired(o.getValue());                    }                }            }        }


     啟動/關閉逾時檢查線程都需要進行封鎖機制,這裡使用的是讀寫鎖:

Java代碼
  1. private
     
    final
     ReadWriteLock stateLock = 
    new
     ReentrantReadWriteLock();  
  2.   
  3.   public
     
    void
     startExpiring()   
  4.   {  
  5.       stateLock.writeLock().lock();  
  6.       try
       
  7.       {  
  8.           if
     (!running)   
  9.           {  
  10.               running = true
    ;  
  11.               expirerThread.start();  
  12.           }  
  13.       }   
  14.       finally
       
  15.       {  
  16.           stateLock.writeLock().unlock();  
  17.       }  
  18.   }  
  19.   public
     
    void
     stopExpiring()   
  20.   {  
  21.       stateLock.writeLock().lock();  
  22.       try
       
  23.       {  
  24.           if
     (running)   
  25.           {  
  26.               running = false
    ;  
  27.               expirerThread.interrupt();  
  28.           }  
  29.       }   
  30.       finally
       
  31.       {  
  32.           stateLock.writeLock().unlock();  
  33.       }  
  34.   }  
      private final ReadWriteLock stateLock = new ReentrantReadWriteLock();        public void startExpiring()         {            stateLock.writeLock().lock();            try             {                if (!running)                 {                    running = true;                    expirerThread.start();                }            }             finally             {                stateLock.writeLock().unlock();            }        }        public void stopExpiring()         {            stateLock.writeLock().lock();            try             {                if (running)                 {                    running = false;                    expirerThread.interrupt();                }            }             finally             {                stateLock.writeLock().unlock();            }        }


     會話逾時監聽者:

Java代碼
  1. private
     
    class
     DefaultExpirationListener 
    implements
      
  2.         ExpirationListener<IoSession> {  
  3.     public
     
    void
     expired(IoSession expiredSession) {  
  4.         expiredSession.close();//關閉逾時的會話
      
  5.     }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.