Mina框架翻译(2011-7月11日)

来源:互联网 发布:淘宝客服的工资由谁发 编辑:程序博客网 时间:2024/06/06 15:34
 

最近研究通信框架,翻译了一些官方文档。

异步通信框架MINA

                             Session

Introduction(简述):

SessionMINA框架的核心,每次客户端创建一个连接到服务器都会在内存中建立一个Session,直到连接断开。

Session 的状态:

Session 的三中状态:

1.连接状态(connected):连接状态,当建立可用连接时。

2.IDLE状态:连接建立后在一段时间没有处理任何请求,时间间隔是可配置的,包括以下 三种况:

   a.Idle for read :一段时间没有读操作。

   b. Idle for wirte:一段时间没有写操作。(no write operation for a period tiem

   c.Idel for both: no read nor wirte for period time

                               

Configration(配置):可以为特定的Session配置制定的参数。

          A. 接收数据的缓存大小(receive buffer size)。

          B. 发送数据的缓存大小(send buffer size)

          C. IdletTimeIdle状态时间)。

          D. Write timeOut (写超时)。

          E. 其他配置依赖于传送数据的类型。

Managing user-defined attributes(管理用户自定义属性):有时候需要在Session里传递数据,可以通过attribute传递任意类型的数据,这种数据是以键值队(key-Value)的方式被传递。

例如,你想跟踪一个用户建立Session后的请求数你就可以创建一个attribute

     ...

    int counterValue = session.getAttribute( "counter" );

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

    ...

用户自定义的属性是以key-value的方式存储在Sessioncontainer(容器)中,我们可以添加和去除定义的属性,container(容器) 在Session建立是创建,在session关闭是销毁。

Defining the container (自定义容器):由于container key-value容器默认是个Map,如果需要存放较大数据,又不希望常驻内存浪费资源,可以实现一个factory接口,在session创建时创建一个Container

以下代码是ContainerSession初始化是如何被创建的:

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

        }

        ...

 

我们可以通过实现下面的接口声明自定义的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(过滤器链):每个session与一系列的过滤器相关联,当有请求进来,或有数据发送出去的时候,都要被filter拦截处理,每个session都拥有独立的过滤器链(非共享),大多数情况下我们为所有的存在session使用相同的过滤器链。

Statistics(统计信息):每个session都能记录以下的统计信息。

A. 发送/接收的数据的字节数。

B. 发送/接收的消息数。

C. Idle 状态。

D.其他信息。

Handler(处理器):session都有一个相关的hanlder负责分发消息到你的应用程序。Hanler也可以通过session发送响应数据包通过write() 方法。

    ...

    session.write( <your message> );

    ...