MINA源码分析---心跳包过滤器KeepAliveFilter


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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值