当前位置: 代码迷 >> 综合 >> mina 添加心跳包
  详细解决方案

mina 添加心跳包

热度:39   发布时间:2023-12-17 21:13:10.0

1.检测心跳包的关键代码如下:setKeepAliveRequestInterval(30);定时进入IoHandlerAdapter子类的sessionIdle(IoSession session, IdleStatus status)回调方法;

/***************************************************************************************//**检测每一个连接的IoSession的心跳包,定时进入Idle状态,用一下方法,以上备注不可行**/KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl();//KeepAliveMessageFactory的实现类KeepAliveFilter kaf = new KeepAliveFilter(kamfi);kaf.setKeepAliveRequestInterval(30); //设置进入Idle状态的时间,单位为sacceptor.getFilterChain().addLast("heart", kaf);/***************************************************************************************/

2.主函数如下:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.common.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.keepalive.KeepAliveFilter;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;public class MinaService {
    private static final int PORT = 22222;// 定义监听端口public static void main(String[] args) throws IOException {IoAcceptor acceptor = new NioSocketAcceptor();// acceptor.getSessionConfig().setReadBufferSize(1024*1024);//发送缓冲区1MObjectSerializationCodecFactory factory = new ObjectSerializationCodecFactory();factory.setDecoderMaxObjectSize(Integer.MAX_VALUE);// 设定后服务器可以接收大数据factory.setEncoderMaxObjectSize(Integer.MAX_VALUE);// 读写 通道均在30 秒内无任何操作就进入空闲状态.// acceptor.setReaderIdleTime(30);// acceptor.setWriterIdleTime(30);// acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 30);/***************************************************************************************//**检测每一个连接的IoSession的心跳包,定时进入Idle状态,用一下方法,以上备注不可行**/KeepAliveMessageFactoryImpl kamfi = new KeepAliveMessageFactoryImpl();KeepAliveFilter kaf = new KeepAliveFilter(kamfi);kaf.setKeepAliveRequestInterval(30); acceptor.getFilterChain().addLast("heart", kaf);/***************************************************************************************/// acceptor.getFilterChain().addLast("logger", new LoggingFilter());acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));// 指定编码过滤器acceptor.setHandler(new TimeServerHandler());// 指定业务逻辑处理器acceptor.setDefaultLocalAddress(new InetSocketAddress(PORT));// 设置端口号acceptor.bind();// 启动监听}
}class KeepAliveMessageFactoryImpl implements KeepAliveMessageFactory {@Overridepublic boolean isRequest(IoSession session, Object message) {return false;}@Overridepublic boolean isResponse(IoSession session, Object message) {return false;}@Overridepublic Object getRequest(IoSession session) {return null;}@Overridepublic Object getResponse(IoSession session, Object request) {return null;}}

3.TimeServerHandler是IoHandlerAdapter的子类: