Apache Mina study notes (4)-Session

黄舟
Release: 2023-03-04 22:20:01
Original
1272 people have browsed it

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.
Copy after login

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 );  
...
Copy after login

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);  
    }
Copy after login

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;  
 }
Copy after login

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
Copy after login

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> );  
...
Copy after login

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)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!