• Apache MINA 框架之Session介绍


    Session是NIMA的核心,每一次客户端连接到服务器将创建一个新的session,它将保存在内存中,知道客户端断开连接。

    Session State

    • Connected : session创建成功并有效
    • Idle : 闲置状态
      • Idle for read : 一段时间内没有读操作;
      • Idle for write : 段时间内没有写操作;
      • Idle for both : 段时间内既没有读也没有写操作;
    • Closing : session正在关闭状态;
    • Closed : session关闭状态;

    session状态转换图:

    session配置:

    不同参数的配置可以组合成一个特定的session

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

    管理用户自定义属性:

    将来可能使用的一些数据可以储存在session中,通过key-value键值对存储,例如:你想知道用户连接的数量:

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

    这样,我们通过在session对属性的key/value进行增删改读的基本操作,实现对自定义属性的处理。

    定义容器:

    容器已key-value键值对存储,默认使用map,但是也可以定义为另外的数据结构如果想处理长时间存活的数据或者避免内存过大。我们可以通过实现接口或工厂的方式在创建session的时候创建好容器。

    创建session的时候创建容器:

    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);
        }
        ...

    通过工厂接口实现其他数据结构容器:

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

    每个session都关联一个过滤链,处理连接请求,发送或接受。可以通过session动态的修改和管理过滤链。

    统计:

    每个session可以跟踪session记录:

    • 发送/接受的字节数
    • 接受/发送的信息数
    • 闲置状态
    • 吞吐量

    等等之类有用的信息....

    Handler(处理器)

    每个session至少依附于一个Handler上,应用程序通过调用session的write()方法发送响应包:

    ...
    session.write( <your message> );
    ...
  • 相关阅读:
    在虚拟机上安装Docker并运行镜像下
    分红包思想
    从微信授权到JWT认证——玩转token之路
    .Net(C#)数据库访问连接方式
    Asp.Net导出Excel表格之二(HttpContext.Current.Response)
    我的ip_本机ip_本地ip_本机ip地址_公网ip_ip地址查询
    干货版“测试小品”欢乐场景
    家用宽带搭建Hmailserver邮箱服务器
    【microPython与esp8266】之一——呼吸灯与PWM
    截取长文本,显示省略号(text-overflow:ellipsis)
  • 原文地址:https://www.cnblogs.com/quyongjin/p/3135754.html
Copyright © 2020-2023  润新知