mina心跳包已经收到回应,但是连接还是会断开,求高手指点

mqwsd 发布于 2016/06/22 13:43
阅读 742
收藏 0

mina客户端做心跳包,可以收到服务端心跳回应,但是还是会断开连接,不知什么问题?(心跳包由客户端主动发起,服务端回应,客户端在一定时间内没有收到回应,则断开连接)代码如下

public class KeepAliveMessageFactoryImpl implements
 KeepAliveMessageFactory {
 
 final Logger logger = Logger.getLogger(KeepAliveMessageFactoryImpl.class);


 @Override
 public Object getRequest(IoSession session) {
 PacketsEntity pe = new PacketsEntity(1,null);
 return pe;
 }


 @Override
 public Object getResponse(IoSession session, Object request) {
 return request;
 }


 @Override
 public boolean isRequest(IoSession session, Object message) {
 return false;
 }


 @Override
 public boolean isResponse(IoSession session, Object message) {
 if(message instanceof PacketsEntity){
 PacketsEntity pe = (PacketsEntity) message;
 if(SignUtil.isSign(pe.getValuePackets(), pe.getSign())){
 return true;
 }
 }
 return false;
 }
 
}

@Service public class MinaClient implements InitializingBean { final Logger logger = Logger.getLogger(MinaClient.class); private IoSession session; private NioSocketConnector connector; public void start() throws IOException { // 读取配置文件停车场Id String host  = ConfUtil.getCloudHost(); int port = ConfUtil.getCloudPort(); connector = new NioSocketConnector(); connector.setConnectTimeoutMillis(10000);      CloudClientProtocolCodecFactory factory = new CloudClientProtocolCodecFactory();         //加入解码器          connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(factory));          connector.getFilterChain().addLast( "logger", new LoggingFilter() );          connector.getSessionConfig().setReceiveBufferSize(1024*1024);   // 设置接收缓冲区的大小          connector.getSessionConfig().setSendBufferSize(1024*1024);// 设置输出缓冲区的大小          connector.getFilterChain().addLast("mdc", new MdcInjectionFilter());           /**mina 心跳机制*/ KeepAliveMessageFactory heartBeatFactory = new KeepAliveMessageFactoryImpl(); KeepAliveFilter heartBeat = new KeepAliveFilter(heartBeatFactory, IdleStatus.READER_IDLE, KeepAliveRequestTimeoutHandler.CLOSE); heartBeat.setForwardEvent(true); heartBeat.setRequestInterval(8); heartBeat.setRequestTimeout(5); connector.getSessionConfig().setKeepAlive(true); connector.getFilterChain().addLast("heart", heartBeat);        //添加处理器          connector.setHandler(new MinaClientHandler());                     connector.setDefaultRemoteAddress(new InetSocketAddress(host, port));// 设置默认访问地址                  connector.getFilterChain().addFirst("reconnection", new IoFilterAdapter() {           @Override           public void sessionClosed(NextFilter nextFilter, IoSession ioSession) throws Exception {           for(;;){           try{           Thread.sleep(5000);           ConnectFuture future = connector.connect();           future.awaitUninterruptibly();// 等待连接创建成功           session = future.getSession();// 获取会话           if(session.isConnected()){           logger.info("断线重连["+ connector.getDefaultRemoteAddress().getHostName() +":"+ connector.getDefaultRemoteAddress().getPort()+"]成功");           break;           }           }catch(Exception ex){           logger.info("重连服务器登录失败,3秒再连接一次:" + ex.getMessage());           }           }           }          });          for (;;) {              try {                  ConnectFuture future = connector.connect();                  // 等待连接创建成功                  future.awaitUninterruptibly();                  // 获取会话                  session = future.getSession();                  /* JSONObject json = new JSONObject();                json.put("tag", "0001");                json.put("parkId", parkId);                session.write(json);*/                logger.error("连接服务端" + host + ":" + port + "[成功]" + ",,时间:" + new SimpleDateFormat("yyyy-MM-dd HH🇲🇲ss").format(new Date()));                  break;              } catch (RuntimeIoException e) {                  logger.error("连接服务端" + host + ":" + port + "失败" + ",,时间:" + new SimpleDateFormat("yyyy-MM-dd HH🇲🇲ss").format(new Date()) + ", 连接MSG异常,请检查MSG端口、IP是否正确,MSG服务是否启动,异常内容:" + e.getMessage(), e);                  try { Thread.sleep(5000); } catch (InterruptedException e1) { e1.printStackTrace(); }            }          } } @Override public void afterPropertiesSet() throws Exception { try { //web项目启动后,连接mina服务器 new MinaClient().start(); } catch (IOException e) { e.printStackTrace(); } }

报错信息

[WARN ] org.apache.mina.filter.keepalive.KeepAliveFilter - Closing the session because a keep-alive response message was not received within 5 second(s).

加载中
返回顶部
顶部