>  기사  >  백엔드 개발  >  Apache Mina 연구 노트(4)-세션

Apache Mina 연구 노트(4)-세션

黄舟
黄舟원래의
2017-01-18 09:59:431225검색

세션은 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 Mina 연구 노트(4)-세션

다음 매개변수는 다음과 같습니다. 세션 구성

수신 버퍼 크기
전송 버퍼 크기
유휴 시간
Write 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

마지막으로 프로그램 메시지를 처리하려면 핸들러를 세션에 연결해야 합니다. 이 Handler는 이에 대한 응답으로 패키지도 보낼 것입니다. 간단히 write 메소드를 호출하면 됩니다:

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

위는 Apache Mina 연구 노트 (4)-세션의 내용입니다. PHP 중국어 홈페이지(www.php.cn)!


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.