Chapter 4 - Session

The Session is at the heart of MINA : every time a client connects to the server, a new session is created, and will be kept in memory until the client is disconnected.

A session is used to store persistent informations about the connection, plus any kind of information the server might need to use during the request processing, and eventually during the whole session life.

Session state

A session has a state, which will evolve during time.

  • 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 state diagram exposes all the possible states and transitions :

Configuration

Many different parameters can be set for a specific session :

  • receive buffer size
  • sending buffer size
  • Idle time
  • Write timeOut

plus other configuration, depending on the transport type used (see Chapter 6 - Transports)

Managing user-defined attributes

It might be necessary to store some data which may be used later. This is done using the dedicated data structure associated which each session. This is a key-value association, which can store any type of data the developer might want to keep remanent.

For instance, if you want to track the number of request a user has sent since the session has been created, it's easy to store it into this map: just create a key that will be associated with this value.

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

We have a way to handle stored Attributes into the session : an Attribute is a key/value pair, which can be added, removed and read from the session's container.

This container is created automatically when the session is created, and will be disposed when the session is terminated.

Defining the container

As we said, this container is a key/value container, which default to a Map, but it's also possible to define another data structure if one want to handle long lived data, or to avoid storing all those data in memory if they are large : we can implement an interface and a factory that will be used to create this container when the session is created.

This snippet of code shows how the container is created during the session initialization :

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 is associated with a chain of filters, which will be processed when an incoming request or an outgoing message is received or emitted. Those filters are specific for each session individually, even if most of the cases, we will use the very same chain of filters for all the existing sessions.

However, it's possible to dynamically modify the chain for a single session, for instance by adding a Logger Filter in the chain for a specific session.

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, not least, a session is attached to a Handler, in charge of dispatching the messages to your application. This handler will also send pack response by using the session, simply by calling the write() method :

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

Mina Session的更多相关文章

  1. session.write类型引发的思考---Mina Session.write流程探索.doc--zhengli

    基于Mina开发网络通信程序,在传感器数据接入领域应用的很广泛,今天我无意中发现一个问题,那就是我在前端session.write(msg)数据出去之后,却没有经过Filter的Encoder方法,同 ...

  2. Mina、Netty、Twisted一起学(六):session

    开发过Web应用的同学应该都会使用session.由于HTTP协议本身是无状态的,所以一个客户端多次访问这个web应用的多个页面,服务器无法判断多次访问的客户端是否是同一个客户端.有了session就 ...

  3. MINA的session.close

    现象:客户端session.close之后,并没有提出,客户端程序一直hold在那里: 解决:调用了session.getService().dispose(false)方法后,客户端程序完成了退出 ...

  4. Apache MINA 框架之默认session管理类实现

    DefaultSocketSessionConfig 类 extends AbstractSocketSessionConfig extends AbstractIoSessionConfig imp ...

  5. Openfire的启动过程与session管理

    说明   本文源码基于Openfire4.0.2.   Openfire的启动       Openfire的启动过程非常的简单,通过一个入口初始化lib目录下的openfire.jar包,并启动一个 ...

  6. JAVA通信系列二:mina入门总结

    一.学习资料 Mina入门实例(一) http://www.cnblogs.com/juepei/p/3939119.html Mina入门教程(二)----Spring4 集成Mina http:/ ...

  7. Mina、Netty、Twisted一起学(八):HTTP服务器

    HTTP协议应该是目前使用最多的应用层协议了,用浏览器打开一个网站就是使用HTTP协议进行数据传输. HTTP协议也是基于TCP协议,所以也有服务器和客户端.HTTP客户端一般是浏览器,当然还有可能是 ...

  8. mina通信 demo

    1,要用到4个jar 2,服务端 package mina.server; import java.io.IOException; import java.net.InetSocketAddress; ...

  9. Mina入门实例(一)

    mina现在用的很多了,之前也有用到,但是毕竟不熟悉,于是查了一些资料,做了一些总结.看代码是最直观的,比什么长篇大论都要好.不过其中重要的理论,也要理解下. 首先是环境,程序运行需要几个包,这里用m ...

随机推荐

  1. android显示和隐藏软键盘

    显示键盘: EditText editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.request ...

  2. java学习笔记----@Override的作用

    初学java或多或少都会有这样的疑问,@Override有什么用,有的时候写,有的时候又不写,搞的初学者甚是郁闷. 做了一两年的开发说起这个问题不一定能够对答如流.小弟才疏学浅,花了点时间,看了一下资 ...

  3. 使用UUID方法生成全球唯一标识

    需要生成唯一字符串,如生成应用标识等,可以直接用java.util.UUID类实现. UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字, ...

  4. cubic与spline插值点处的区别

    cubic与spline都是Matlab的三次样条插值法,但是它们在插值点处仍然有着很微妙的区别,这个区别说明不了两种方法的好坏,只能根据实际情况进行合理筛选.以一维插值为例: clc clear % ...

  5. np.hsplit()

    numpy.hsplit numpy.hsplit(ary, indices_or_sections)[source] Split an array into multiple sub-arrays ...

  6. In case of failure

    In case of failure http://acm.hdu.edu.cn/showproblem.php?pid=2966 Time Limit: 60000/30000 MS (Java/O ...

  7. docker搭建nginx

    在Docker下部署Nginx,包括: 部署一个最简单的Nginx,可以通过端口访问默认的网站 设置记录访问和错误日志的路径 以交互模式创建centos容器,如果本地没有镜像,会从仓库获取, 等待即可 ...

  8. 表单数据转换成json格式数据

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. MVC仓储使用join

    代码: var result = from mpc in this.Context.Set<Domain.S_MENU_PURVIEWCODE>() join menu in this.C ...

  10. Ps中的难点问题分析

    一.布尔运算的运用 1.布尔运算是在图形工具组中使用,快捷键“U” 2.使用方法:都是在同一图层下运算,在进行布尔运算之前,首先用路径选择工具,小黑箭头,快捷键是“A” 选取你要运算的图形. 3.布尔 ...