ApacheMina study notes (4)-Session

Source: Internet
Author: User
Session is the core of Apache. every time a client is connected, a new Session is created until the connection is closed. Session is used to save connections and various information. Session is the core of Apache. every time a client is connected, a new Session is created until the connection is closed. Session is used to save connections and various information.

The Session has the following statuses:

Connected : the session has been created and is availableIdle : 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 timeIdle for write : no write has actually been made for a period of timeIdle for both : no read nor write for a period of timeClosing : 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.

Indicates the status transition relationship of the Session:

For example, if you want to track how many requests a user sends after a session is established, it can easily store the ing: you only need to create a key and associate it with the value.

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

We use the key/value pair 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, which is a ING by default and can also be defined as other data structures. When a Session is created, we can implement an interface and a factory to create a container. The following code snippet demonstrates how to create a container during Session initialization (you cannot understand it. 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 session.  Please note that the returned      * implementation must be thread-safe.      */       IoSessionAttributeMap getAttributeMap(IoSession session) throws Exception;   }

Filter chain

Each session is associated with a filter chain to process incoming requests or outgoing data. Each session will specify a separate filtering chain. In most cases, we will use many identical filtering chains in the session.

Statistics

Each session also keep a track of records about what has been done for the session :number of bytes received/sentnumber of messages received/sentIdle statusthroughputand many other useful informations.Handler

Finally, it is equally important that a Handler is attached to a Session to process messages of the program. This Handler will also send a packet as a response, as long as the write method is simply called:

...  session.write( 
 
   );  ...
 

The above is the Apache Mina study note (4)-Session content. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.