Apache Mina Learning Notes (4)-session

Source: Internet
Author: User
The session is the core of Apache, and whenever a client connection arrives, a new session is created until the connection is closed. The session is used to save the connection, as well as various information.

The session has the following States:

Connected:the session has been created and was availableidle:the session hasn ' t processed any request for at least a PE Riod of time (this period was configurable) Idle for Read:no read have actually been made for a period of timeidle for writ E:no write have actually been made for a period of timeidle for Both:no read nor write for a period of timeclosing:th  E session is being closed (the remaining messages was being flushed, cleaning up isn't terminated) Closed:the session is Now closed, nothing else can is done to revive it.

Represents the state transition relationship for a session:

Several parameters can be used to configure the session

Receive Buffer size
Sending buffer size
Idle time
Write TimeOut

Manage user-defined properties:

For example, if you want to track how many requests a user has sent since the session was established, it can easily be stored in such a mapping: simply create a key to associate to value on the line.

...  int countervalue = Session.getattribute ("counter");  Session.setattribute ("Counter", Countervalue + 1);  ...

We use Key/value to store properties in the session, and this key/value can be read, added, or deleted by the container through the session.

Define Container

As we said, this container is a key/value container, which is a mapping by default, and of course can 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 is an example of how to create a container when the session is initialized (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 are the factory interface we can implement if we want to define another kind of container:

Public interface Iosessiondatastructurefactory {      /**      * Returns a {@link iosessionattributemap} which is going to Be associated      * with the specified <tt>session</tt>.  Please note that the returned      * implementation must is Thread-safe.      *       /Iosessionattributemap Getattributemap (iosession session) throws Exception;   }

Filter Chain

Each session is associated with a filter chain to handle incoming requests or data that goes out. Each session will specify a separate filter chain, and in most cases we will use many of the same filter chains in the session.

Statistics

Each session also keep a track of records about what have been done for the session:number of bytes Received/sentnumber of Messages Received/sentidle Statusthroughputand Many other useful informations. Handler

Finally, it is also important that a handler be attached to a session that is used to process the message of the program. This handler also sends a packet as a response, as long as it simply calls the Write method:

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

The above is the Apache Mina study Note (4)-session content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.