Home  >  Article  >  Backend Development  >  Apache Mina study notes (4)-Session

Apache Mina study notes (4)-Session

黄舟
黄舟Original
2017-01-18 09:59:431226browse

Session is the core of Apache. Whenever a client connection arrives, a new Session will be created until the connection is closed. Session is used to save connections and various information.

Session has the following states:

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.

The following figure shows the state transition relationship of Session:

Apache Mina study notes (4)-Session

The following parameters can be used To configure Session

receive buffer size
sending buffer size
Idle time
Write timeOut

Manage user-defined attributes:

For example, if If you want to track how many requests a user has sent since the session was established, it can be easily stored in this map: just create a key and associate it with the value.

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

We use key/value pairs to store attributes in the session. This key/value pair can be read, added or deleted through the session container.

Define container

As we said, this container is a key/value container. The default is a mapping. Of course, it can also be defined as other data structures. When the Session is created we can implement an interface and a factory to create the container. The following code snippet illustrates how to create a container during Session initialization (I don’t understand, what does this mean?)

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

Filter chain

Each session will be associated with some filter chains to process incoming requests or outgoing data. Each session will have a separate filter chain specified, and in most cases we will use many of the same filter chains across sessions.

Statistics

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

Last but not least, a Handler must be attached to a Session to handle program messages. This Handler will also send a package in response, just simply call the write method:

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

The above is the content of Apache Mina study notes (4)-Session. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn