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