MINA框架中原生态心跳支持
??????MINA本身自带了对心跳协议的支持,可以对心跳做出细致的配置,昨天仔细研究了一下MINA的API文档,翻译了一下其中心跳的内容(翻译的不好请大家见谅),做了一个测试例子。和需要用到MINA此功能的同行一起探讨一下。
?
Active活跃型你希望当读取者空闲时发送心跳消息。一旦请求发出,对心跳的响应必须在keepAliveRequestTimeout seconds的时间内.接收。否则,指定的KeepAliveRequestTimeoutHandler 类会被调用。如果心跳请求被接收到,它的响应当然会被回复。Both KeepAliveMessageFactory.getRequest(IoSession) and
KeepAliveMessageFactory.getResponse(IoSession, Object)
must return a non-null.
?Semi-active
半活跃型?你希望当读取者空闲时发送心跳消息。然而,你实质上并不在意它是否会得到响应。如果心跳请求被接收,同时他会把回应发回来。?Both KeepAliveMessageFactory.getRequest(IoSession) and
KeepAliveMessageFactory.getResponse(IoSession, Object)
must return a non-null,
and the timeoutHandler property should be set to KeepAliveRequestTimeoutHandler.NOOP, KeepAliveRequestTimeoutHandler.LOG or
the custom KeepAliveRequestTimeoutHandler implementation
that doesn't affect the session state nor throw an exception.
?Passive
被动型?你并不想自己发送心跳请求,但是如果由心跳请求被接收到还是会发响应回去。?KeepAliveMessageFactory.getRequest(IoSession) must return null and KeepAliveMessageFactory.getResponse(IoSession, Object) must return a non-null.?Deaf Speaker
聋子型?当读取者空闲时你希望发出心跳请求,但是你并不希望得到任何反馈。?KeepAliveMessageFactory.getRequest(IoSession) must return a non-null, KeepAliveMessageFactory.getResponse(IoSession, Object) must return null and the timeoutHandler must be set to KeepAliveRequestTimeoutHandler.DEAF_SPEAKER.?Silent Listener
沉默监听型?你不想发出心跳请求,同时你并不想收到任何回应。?Both KeepAliveMessageFactory.getRequest(IoSession) and KeepAliveMessageFactory.getResponse(IoSession, Object) must return null.写道请注意你务必正确实现KeepAliveMessageFactory.isRequest(IoSession, Object) and KeepAliveMessageFactory.isResponse(IoSession, Object)中任何一个你选择实现的方法.?
以上是MINA的?心跳包API,从文档中可以得知MINA的心跳主要是依赖KeepAliveMessageFactory,和KeepAliveRequestTimeoutHandler的实现。。
?
附上自己测试的代码。
?
?
?
1 楼 xj_java_8931 2011-11-14 感谢!好例子。我用mina2可以正常运行。