首頁 後端開發 php教程 Apache Mina 學習筆記(4)-Session

Apache Mina 學習筆記(4)-Session

Jan 18, 2017 am 09:59 AM
apache Mina session

Session是Apache的核心,每當一個客戶端連線到達時,就會有一個新的Session被創建,直到該連線關閉。 Session被用來保存連接,以及各種資訊。

Session有以下幾種狀態:

Connected : the session has been created and is available
Idle : the session hasn't processed any request for at least a period of time (this period is configurable)

Idle for read : no read has actually been made for a period of time
Idle for write : no write has actually been made for a period of time
Idle for both : no read nor write for a period of time

Closing : the session is being closed (the remaining messages are being flushed, cleaning up is not terminated)
Closed : The session is now closed, nothing else can be done to revive it.
登入後複製

下圖表示Session的狀態轉換關係:

Apache Mina 學習筆記(4)-Session

以下幾個參數可以用來設定Session

receive buffer sizesize

管理用戶自訂的屬性:

例如,如果你想追蹤一個用戶從會話被建立之後發送了多少個請求,那麼它可以很容易存入這種映射:只要創建一個key關聯到value就行。

...  
int counterValue = session.getAttribute( "counter" );  
session.setAttribute( "counter", counterValue + 1 );  
...
登入後複製

我們採用key/value 對的方式儲存屬性到會話中,這種key/value對可以透過session的容器讀取,添加或刪除。

定義container


正如我們所說,這個容器是一個key/value容器,預設是一種映射,當然也可以定義成其他的資料結構。當Session被創建時我們可以實作一個介面和一個factory用來建立容器。下面的程式碼片段範例了在Session初始化時如何建立容器(看不懂,這個到底什麼意思?)

protected final void initSession(IoSession session,  
        IoFuture future, IoSessionInitializer sessionInitializer) {  
    ...  
    try {  
        ((AbstractIoSession) session).setAttributeMap(session.getService()  
                .getSessionDataStructureFactory().getAttributeMap(session));  
    } catch (IoSessionInitializationException e) {  
        throw e;  
    } catch (Exception e) {  
        throw new IoSessionInitializationException(  
                "Failed to initialize an attributeMap.", e);  
    }
登入後複製

and here is the factory interface we can implement if we want to define another kind of container :
public interface IoSessionDataStructureFactory {  
    /** 
     * Returns an {@link IoSessionAttributeMap} which is going to be associated 
     * with the specified <tt>session</tt>.  Please note that the returned 
     * implementation must be thread-safe. 
     */  
     IoSessionAttributeMap getAttributeMap(IoSession session) throws Exception;  
 }
登入後複製

Each session also keep a track of records about what has been done for the session :

number of bytes received/sent
number of messages received/sent
Idle status
throughput

and many other useful informations.

Handler
登入後複製

過濾鏈

每個會話會關聯一些過濾鏈,用來處理到來的請求或出去的資料。每個會話都會指定單獨的過濾鏈,大多數情況下,我們會用在會話中用很多相同的過濾鏈。

統計

...  
session.write( <your message> );  
...
登入後複製

最後,同樣重要的是,一個Handler要附著於一個Session上,用來處理程序的消息。這個Handler也會發送套件作為回應,只要簡單的呼叫write方法即可:rrreee

以上就是Apache Mina 學習筆記(4)-Session的內容,更多相關內容請關注PHP中文網(www.php.cn )!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
apache中cgi目錄怎麼設置 apache中cgi目錄怎麼設置 Apr 13, 2025 pm 01:18 PM

要在 Apache 中設置 CGI 目錄,需要執行以下步驟:創建 CGI 目錄,如 "cgi-bin",並授予 Apache 寫入權限。在 Apache 配置文件中添加 "ScriptAlias" 指令塊,將 CGI 目錄映射到 "/cgi-bin" URL。重啟 Apache。

怎麼查看自己的apache版本 怎麼查看自己的apache版本 Apr 13, 2025 pm 01:15 PM

有 3 種方法可在 Apache 服務器上查看版本:通過命令行(apachectl -v 或 apache2ctl -v)、檢查服務器狀態頁(http://&lt;服務器IP或域名&gt;/server-status)或查看 Apache 配置文件(ServerVersion: Apache/&lt;版本號&gt;)。

apache怎麼連接數據庫 apache怎麼連接數據庫 Apr 13, 2025 pm 01:03 PM

Apache 連接數據庫需要以下步驟:安裝數據庫驅動程序。配置 web.xml 文件以創建連接池。創建 JDBC 數據源,指定連接設置。從 Java 代碼中使用 JDBC API 訪問數據庫,包括獲取連接、創建語句、綁定參數、執行查詢或更新以及處理結果。

apache80端口被佔用怎麼辦 apache80端口被佔用怎麼辦 Apr 13, 2025 pm 01:24 PM

當 Apache 80 端口被佔用時,解決方法如下:找出佔用該端口的進程並關閉它。檢查防火牆設置以確保 Apache 未被阻止。如果以上方法無效,請重新配置 Apache 使用不同的端口。重啟 Apache 服務。

怎麼查看apache版本 怎麼查看apache版本 Apr 13, 2025 pm 01:00 PM

如何查看 Apache 版本?啟動 Apache 服務器:使用 sudo service apache2 start 啟動服務器。查看版本號:使用以下方法之一查看版本:命令行:運行 apache2 -v 命令。服務器狀態頁面:在 Web 瀏覽器中訪問 Apache 服務器的默認端口(通常為 80),版本信息顯示在頁面底部。

apache怎麼配置zend apache怎麼配置zend Apr 13, 2025 pm 12:57 PM

如何在 Apache 中配置 Zend?在 Apache Web 服務器中配置 Zend Framework 的步驟如下:安裝 Zend Framework 並解壓到 Web 服務器目錄中。創建 .htaccess 文件。創建 Zend 應用程序目錄並添加 index.php 文件。配置 Zend 應用程序(application.ini)。重新啟動 Apache Web 服務器。

apache不能啟動怎麼解決 apache不能啟動怎麼解決 Apr 13, 2025 pm 01:21 PM

Apache 無法啟動,原因可能有以下幾點:配置文件語法錯誤。與其他應用程序端口衝突。權限問題。內存不足。進程死鎖。守護進程故障。 SELinux 權限問題。防火牆問題。軟件衝突。

apache怎麼刪除多於的服務器名 apache怎麼刪除多於的服務器名 Apr 13, 2025 pm 01:09 PM

要從 Apache 中刪除多餘的 ServerName 指令,可以採取以下步驟:識別並刪除多餘的 ServerName 指令。重新啟動 Apache 使更改生效。檢查配置文件驗證更改。測試服務器確保問題已解決。

See all articles