ホームページ  >  記事  >  バックエンド開発  >  Apache min 学習ノート (4) - セッション

Apache min 学習ノート (4) - セッション

黄舟
黄舟オリジナル
2017-01-18 09:59:431226ブラウズ

セッションは Apache のコアです。クライアント接続が到着すると、接続が閉じられるまで新しいセッションが作成されます。セッションは接続やさまざまな情報を保存するために使用されます。

セッションには以下の状態があります:

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.

次の図はセッションの状態遷移関係を示しています:

Apache min 学習ノート (4) - セッション

以下のパラメータはセッションの設定に使用できます

受信バッファサイズ
送信バッファサイズ
アイドル時間
書き込みtimeOut

ユーザー定義属性の管理:

たとえば、セッションが確立されてからユーザーが送信したリクエストの数を追跡したい場合は、このマッピングに簡単に保存できます。キーを作成してそれを関連付けるだけです。値。

...  
int counterValue = session.getAttribute( "counter" );  
session.setAttribute( "counter", counterValue + 1 );  
...

キーと値のペアを使用して、セッションに属性を保存します。このキーと値のペアは、セッション コンテナーを通じて読み取り、追加、削除できます。

コンテナを定義する

前述したように、このコンテナはキー/値コンテナであり、デフォルトではマッピングになります。もちろん、他のデータ構造として定義することもできます。セッションが作成されると、コンテナを作成するためのインターフェイスとファクトリを実装できます。次のコード スニペットは、セッションの初期化中にコンテナを作成する方法を示しています (理解できません、これはどういう意味ですか?)

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);  
    }

。また、別の種類のコンテナを定義したい場合に実装できるファクトリ インターフェイスは次のとおりです:

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

最後になりましたが、プログラム メッセージを処理するにはハンドラーをセッションにアタッチする必要があります。このハンドラーは、単に write メソッドを呼び出すだけで、応答としてパッケージも送信します:

...  
session.write( <your message> );  
...

上記は、Apache Min 学習ノート (4) - セッションの内容です。その他の関連コンテンツについては、PHP 中国語 Web サイト ( www.php.cn )!


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。