暮夜望日 2016-04-01 07:28 采纳率: 100%
浏览 2263
已采纳

mina心跳的实现和业务逻辑之间的问题

 @Override
    protected boolean doDecode(IoSession session, IoBuffer buffer,
            ProtocolDecoderOutput out) throws Exception {
        int position = buffer.position();
        //第一次读取数据的时候
        if(session.getAttribute("fileName")==null){
            session.setAttribute("fileName",buffer.getInt());
            session.setAttribute("fileContent", buffer.getInt());
            return false;
        }
        else{
            int fileName = (Integer) session.getAttribute("fileName");
            int fileContent = (Integer) session.getAttribute("fileContent");
            //数据全部传输完,开始解码
            if(buffer.limit()-buffer.position()>=fileContent+fileName){
                byte[] nameByte = new byte[fileName]; 
                byte[] contentByte = new byte[fileContent]; 
                PictureBean picture = new PictureBean();
                int start = buffer.position();
                int limit = buffer.limit();
                buffer.limit(start+fileName);
                buffer.slice().get(nameByte);
                picture.setFileName(nameByte);
                buffer.position(start+fileName);
                buffer.limit(limit);
                buffer.slice().get(contentByte);
                picture.setFileContent(contentByte);
                session.removeAttribute("fileName");
                session.removeAttribute("fileContent");
                out.write(picture);
                return true;
            }
            buffer.position(position);
            return false;
        }
}

刚刚学mina,用mina来传输文件可以了,客户端的encoder和服务端的Decoder是自定义的。
但是在想加入心跳的时候发现,心跳的内容会和业务的内容冲突,就是解码器那里很难把这两个完全分开。没用XMPP协议。想问下怎么不影响业务的情况下实现心跳

  • 写回答

1条回答

  • 风聆水净 2016-04-01 08:28
    关注

    心跳包内容是固定的啊。在解码完成后,可以判断出来了。
    我记得可以添加心跳类,里面有判断吧。

     KeepAliveMessageFactory heartBeatFactory = new IMKeepAlive();
            // 设置时间
            KeepAliveFilter keepAliveFilter = new KeepAliveFilter(heartBeatFactory,
                    IdleStatus.BOTH_IDLE, KeepAliveRequestTimeoutHandler.CLOSE,
                    IDLE_TIME, TIME_OUT);
    

    第一句话忽略吧。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配