IoService 介紹 session
IoService 在mina中提供基礎的I/O服務,管理I/O會話(Session).是MINA框架中一個很關鍵的組成部分。大部分的底層的I/O操做,都是由IoService及其子接口的實現類完成的。app
(Base interface for all IoAcceptor and IoConnector that provide I/O service and manage IoSession.)框架
職責 :socket
如上圖所示, IoService包含了許多的職責 :ide
sessions (會話)管理 : 建立、刪除 sessions, 檢測空閒
filter chain(過濾器)管理 : 處理過濾鏈,容許用戶在傳輸中改變過濾器 (allowing the user to change the chain on the fly)
回調handler : 當有信息接受是調用handler, etc
統計信息管理statistics management : 更新消息、字節發送數量等
監聽器管理listeners management : 管理用戶設置的監聽器
通訊管理communication management : 管理兩端的數據傳輸ui
Interface Details 接口信息this
TransportMetadata getTransportMetadata()spa
這個方法獲取傳輸方式的元數據描述信息,也就是底層到底基於什麼的實現,譬如: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 對象.3d
IoService Details server
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 :