1、源码中都加了注释了,用户所要做的就是定义自己的心跳包信息,以及判断拉收的信息是否是心跳包,封装心跳包信息
2、实现KeepAliveMessageFactory接口,必要时可以继承KeepAliveFilter类,扩展功能都是可以的,也可以定义自己的心跳过滤器
3、接口KeepAliveRequestTimeoutHandler中定义了几个处理获取心跳响应超时的处理,当然也可自己写处理函数,都是非常好扩展的
下面是KeepAliveFilter过滤器类
package org.apache.mina.filter.keepalive;
import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.filterchain.IoFilterChain;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoEventType;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.session.IoSessionConfig;
import org.apache.mina.core.write.DefaultWriteRequest;
import org.apache.mina.core.write.WriteRequest;
/**
* An {@link IoFilter} that sends a keep-alive request on
* {@link IoEventType#SESSION_IDLE} and sends back the response for the
* sent keep-alive request.
*
* <h2>Interference with {@link IoSessionConfig#setIdleTime(IdleStatus, int)}</h2>
*
* This filter adjusts <tt>idleTime</tt> of the {@link IdleStatus}s that
* this filter is interested in automatically (e.g. {@link IdleStatus#READER_IDLE}
* and {@link IdleStatus#WRITER_IDLE}.) Changing the <tt>idleTime</tt>
* of the {@link IdleStatus}s can lead this filter to a unexpected behavior.
* Please also note that any {@link IoFilter} and {@link IoHandler} behind
* {@link KeepAliveFilter}在 KeepAliveFilter过滤器后面的过滤器和处理模块IoHandler
* 将不会接收到任何的会话空闲事件,因为它已经被KeepAliveFilter过滤器截取,(默认情况下)
* 不再在过滤链中向后传递,当然可以通过调用setForwardEvent(boolean forwardEvent)函数设置
* 传递此事件
* will not get any {@link IoEventType#SESSION_IDLE}
* event. To receive the internal {@link IoEventType#SESSION_IDLE} event,
* you can call {@link #setForwardEvent(boolean)} with <tt>true</tt>.
*
* <h2>Implementing {@link KeepAliveMessageFactory}</h2>
* 我们需要自己实现 KeepAliveMessageFactory 接口,定义自己的心跳包,
* 从而在信息传输过来时判断此条信息是不是心跳包信息
* To use this filter, you have to provide an implementation of
* {@link KeepAliveMessageFactory}, 决定是不是心跳包信息
* which determines a received or sent
* message is a keep-alive message or not and creates a new keep-alive
* message:
*
* <table border="1">
* <tr>
* <th>Name</th><th>Description</th><th>Implementation</th>
* </tr>
* <tr valign="top">
* <td>Active活动状态,当会话空闲时,发送心跳包,也期望接收到回复</td>
* <td>You want a keep-alive request is sent when the reader is idle.
* Once the request is sent, the response for the request should be
* received within <tt>keepAliveRequestTimeout</tt> seconds. Otherwise,
* the specified {@link KeepAliveRequestTimeoutHandler} will be invoked.
* If a keep-alive request is received, its response also should be sent back.
* </td>
* <td>Both {@link KeepAliveMessageFactory#getRequest(IoSession)} and
* {@link KeepAliveMessageFactory#getResponse(IoSession, Object)} must
* return a non-<tt>null</tt>.</td>
* </tr>
* <tr valign="top">
* <td>Semi-active半活动状态,发送心跳包,不关心对方是否真正收到</td>
* <td>You want a keep-alive request to be sent when the reader is idle.
* However, you don't really care if the response is received or not.
* If a keep-alive request is received, its response should
* also be sent back.
* </td>
* <td>Both {@link KeepAliveMessageFactory#getRequest(IoSession)} and
* {@link KeepAliveMessageFactory#getResponse(IoSession, Object)} must
* return a non-<tt>null</tt>, and the <tt>timeoutHandler</tt> property
* should be set to {@link KeepAliveRequestTimeoutHandler#NOOP},
* {@link KeepAliveRequestTimeoutHandler#LOG} or the custom {@link KeepAliveRequestTimeoutHandler}
* implementation that doesn't affect the session state nor throw an exception.
* </td>
* </tr>
* <tr valign="top">
* <td>Passive被动的</td>
* <td>You don't want to send a keep-alive request by yourself, but the
* response should be sent back if a keep-alive request is rec