mina 基础三 IoService

   IoService 介绍 

   IoService 在mina中提供基础的I/O服务,管理I/O会话(Session).是MINA框架中一个很关键的组成部分。大部分的底层的I/O操作,都是由IoService及其子接口的实现类完成的。

   (Base interface for all  IoAcceptor and IoConnector that provide I/O service and manage  IoSession.)

   

    

  

职责 :

如上图所示,  IoService包含了许多的职责 :

sessions (会话)管理 : 创建、删除  sessions, 检测空闲
filter chain(过滤器)管理 : 处理过滤链,允许用户在传输中改变过滤器 (allowing the user to change the chain on the fly)
回调handler  : 当有信息接受是调用handler, etc
统计信息管理statistics management : 更新消息、字节发送数量等
监听器管理listeners management : 管理用户设置的监听器
通信管理communication management : 管理两端的数据传输

Interface Details  接口信息

TransportMetadata   getTransportMetadata()

这个方法获取传输方式的元数据描述信息,也就是底层到底基于什么的实现,譬如:nio、apr 等。
void addListener(IoServiceListener listener)
这个方法可以为IoService 增加一个监听器,用于监听IoService 的创建、活动、失效、空闲、销毁,具体可以参考IoServiceListener 接口中的方法,这为你参与IoService 的生命周期提供了机会。
void removeListener(IoServiceListener listener)
这个方法用于移除addListener的方法添加的监听器。
boolean isDisposing()
返回 isDisposed() 的状态,当且仅当 isDisposed() 方法被调用完毕才返回TRUE
boolean isDisposed()
返回service的状态,当且仅当service 的当前进程的所有资源已经释放完毕才返回 TRUE
void dispose()
这个方法用于释放service分配的资源,他可能要花费一些时间,用户应该调用 isDisposing() 和 isDisposed() 判断资源是否释放完成,当一个service被关闭的时候都应该调用该方法来进行资源释放
IoHandler getHandler()
返回当前进程serbice关联的handler
void setHandler(IoHandler handler)
这个方法用于向IoService 注册IoHandler,同时有getHandler()方法获取Handler
Map<Long,IoSession> getManagedSessions()
这个方法获取IoService 上管理的所有IoSession,Map 的key 是IoSession 的id。
int getManagedSessionCount()
返回当前service 上绑定的session数量
IoSessionConfig getSessionConfig()
这个方法用于获取IoSession 的配置对象,通过IoSessionConfig 对象可以设置Socket 连接的一些选项。
IoFilterChainBuilder getFilterChainBuilder()
返回当前service的拦截器连,用户可以在改链上添加自己的过滤器,在service绑定之前
void setFilterChainBuilder(IoFilterChainBuilder builder)
定义service的拦截器链
DefaultIoFilterChainBuilder  getFilterChain()
返回当前service默认的那个filterchain,是getFilterChainBuilder() 快捷方式
boolean isActive()
当前service是否在活动
long getActivationTime()
返回这个service 被激活现在的时间,也就是service存活了多久.  如果这个service 不是活动状态咋返回它最好一次活动的时间
Set<WriteFuture> broadcast(Object message)
向所有注册了的session 广播消息
void setSessionDataStructureFactory(IoSessionDataStructureFactory sessionDataStructureFactory)
 向新注册的service 放一些初始化的数据
int getScheduledWriteMessages()
返回信息数量 (这里的信息时在内存等待socket向外写的)
IoServiceStatistics  getStatistics()
返回service的 IoServiceStatistics 对象.

IoService Details 

  IoService  接口及其子接口IoAcceptor  IoConnector 的实现类是MINA中最重要的类

 (IoService is an interface that is implemented by the two most important classes in MINA :  IoAcceptor  IoConnector)


In order to build a server, you need to select an implementation of the IoAcceptor interface. For client applications, you need to implement an implementation of the IoConnector interface.

IoAcceptor

Basically, this interface is named because of the accept() method, responsible for the creation of new connections between a client and the server. The server accepts incoming connection requests.

At some point, we could have named this interface 'Server' (and this is the new name in the coming MINA 3.0).

As we may deal with more than one kind of transport (TCP/UDP/...), we have more than one implementation for this interface. It would be very unlikely that you need to implement a new one.

We have many of those implementing classes

NioSocketAcceptor : the non-blocking Socket transport IoAcceptor
NioDatagramAcceptor : the non-blocking UDP transport IoAcceptor
AprSocketAcceptor : the blocking Socket transport IoAcceptor, based on APR
VmPipeSocketAcceptor : the in-VM IoAcceptor
Just pick the one that fit your need.

Here is the class diagram for the IoAcceptor interfaces and classes :

 

IoConnector

As we have to use an IoAcceptor for servers, you have to implement the IoConnector for clients. Again, we have many implementation classes :

NioSocketConnector : the non-blocking Socket transport IoConnector
NioDatagramConnector : the non-blocking UDP transport IoConnector
AprSocketConnector : the blocking Socket transport IoConnector, based on APR
ProxyConnector : a IoConnector providing proxy support
SerialConnector : a IoConnector for a serial transport
VmPipeConnector : the in-VM IoConnector
Just pick the one that fit your need.

Here is the class diagram for the IoConnector interfaces and classes :

 

posted @ 2015-04-02 11:16  杨牧远  阅读(828)  评论(0编辑  收藏  举报