基于Mina开发网络通信程序,在传感器数据接入领域应用的很广泛,今天我无意中发现一个问题,那就是我在前端session.write(msg)数据出去之后,却没有经过Filter的Encoder方法,同样能够写入远程服务器。因为我所发送的数据不需要很复杂的编码,所以encoder方法也一直没有去看,今天发现无法被自己写的过滤器所编码,针对这个问题,我打开以前的代码以及以前的项目中的相关代码,有些同事也是session.write(IoBuffer)之后,在encoder方法里面还加上了一句out.write(message);通过跟踪Mina源码发现,session写出去的数据类型是IoBuffer格式的,就不经过自定义的过滤器了。所以下面的代码压根是多余的

  1. @Override
  2. public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
  3. out.write(message);//IoBuffer格式写出去之后,跳过了encoder.
  4. }

下面我把自己跟踪调试Mina的过程记录下来. 
一、场景 
客户端需要每隔Time时间向服务端发送心跳包,代码如下: 
session.write(IoBuffer.wrap("心跳包XXX".getBytes())); 
二、现象 
MyFilter中的Encoder方法encoder不执行

  1. public class MyFilter implements ProtocolCodecFactory {
  2. private ProtocolEncoder encoder = new MyEncoder();
  3. private ProtocolDecoder decoder = new MyDecoder();
  4. @Override
  5. public ProtocolEncoder getEncoder(IoSession session) throws Exception {
  6. return encoder;
  7. }
  8. @Override
  9. public ProtocolDecoder getDecoder(IoSession session) throws Exception {
  10. return decoder;
  11. }
  12. }

三、分析 
进入session.write方法,实现IoSession.write方法的是AbstractIoSession。直接调用的是

  1. public WriteFuture write(Object message) {
  2. return write(message, null);
  3. }

而AbstractIoSession.write(Object message, SocketAddress address) 
该方法的工作流程是:

  • 创建WriteFeature对象,用于返回值(session.write本身就是返回writeFeature)
  • 将session.write(message)中的Object类型的message封装成writeRequest.
  • 启动write动作,这个主要是IoFilterChain来完成的。

具体的核心代码如下:

  1. // Now, we can write the message. First, create a future
  2. WriteFuture writeFuture = new DefaultWriteFuture(this);
  3. WriteRequest writeRequest = new DefaultWriteRequest(message, writeFuture, remoteAddress);
  4. // Then, get the chain and inject the WriteRequest into it
  5. IoFilterChain filterChain = getFilterChain();
  6. filterChain.fireFilterWrite(writeRequest);

继续跟踪到fireFilterWrite里面去,可知IoFilterChain的默认实现类DefaultIoFilterChain中的关键方法:

  1. public void fireFilterWrite(WriteRequest writeRequest) {
  2. Entry tail = this.tail;
  3. callPreviousFilterWrite(tail, session, writeRequest);
  4. }

在这里先要介绍一下DefaultIoFiterChain的数据格式,主要的属性如下:

  1. private final Map<String, Entry> name2entry = new ConcurrentHashMap<String, Entry>();
  2. /** The chain head */
  3. private final EntryImpl head;
  4. /** The chain tail */
  5. private final EntryImpl tail;

其中 head与tail都是DefaultIoFilterChain固有的属性,name2entity是我们为FilterChain添加的过滤器。因而IoFilterChain是用一个链表来保存过滤器的(('tail', prev: 'myFilter:ProtocolCodecFilter', next: 'null')),其中表头和表位都是固定的head和tail,他们对应的Filter也是专有的,HeadFilter和TailFilter. 
关键方法是callPreviousFilterWrite(tail, session, writeRequest);

  1. try {
  2. IoFilter filter = entry.getFilter();
  3. NextFilter nextFilter = entry.getNextFilter();
  4. filter.filterWrite(nextFilter, session, writeRequest);
  5. } catch (Throwable e) {
  6. writeRequest.getFuture().setException(e);
  7. fireExceptionCaught(e);
  8. }

从上面两个代码片段中,可以看出,IoFilterChain首先从列表中找到tail,从tail开始查找filter,顺序调用每个filter的filterWrite()方法。这里的‘顺序调用’,指的是从tail->head调用,也就是逆向调用Filter。但是看到filter.filterWrite(nextFilter, session, writeRequest);这行代码中的参数可以发现,nextFilter,表面的意思是下一个过滤器,有点误解,感觉tail下一个过滤器不就是null吗,其实不然,进入filterWriter可知。

  1. Entry nextEntry = EntryImpl.this.prevEntry;
  2. callPreviousFilterWrite(nextEntry, session, writeRequest);

对于除head和tail过滤器外,其他的过滤器是如何工作的呢?我们看看ProtocolCodecFilter中的fireFilter方法,做了这样的处理:

  1. if ((message instanceof IoBuffer) || (message instanceof FileRegion)) {
  2. nextFilter.filterWrite(session, writeRequest);
  3. return;
  4. }

到这里,就明白了为什么session.write(IoBuffer.wrap())这样写出去,无法经过自己定义的过滤器了,原来在fireFilter中,对message做了判断,如果已经是IoBuffer类型的,就直接return了。 
最后执行的是HeadFilter的fireFilter方法,直接看内容:

  1. if (writeRequest.getMessage() instanceof IoBuffer) {
  2. IoBuffer buffer = (IoBuffer) writeRequest.getMessage();
  3. // I/O processor implementation will call buffer.reset()
  4. // it after the write operation is finished, because
  5. // the buffer will be specified with messageSent event.
  6. buffer.mark();
  7. int remaining = buffer.remaining();
  8. if (remaining == 0) {
  9. // Zero-sized buffer means the internal message
  10. // delimiter.
  11. s.increaseScheduledWriteMessages();
  12. } else {
  13. s.increaseScheduledWriteBytes(remaining);
  14. }
  15. } else {
  16. s.increaseScheduledWriteMessages();
  17. }
  18. s.getWriteRequestQueue().offer(s, writeRequest);
  19. if (!s.isWriteSuspended()) {
  20. s.getProcessor().flush(s);
  21. }

WriteRequestQueue的默认实现就是java.util.concurrent.ConcurrentLinkedQueue,舍去传入的session对象。

session.write类型引发的思考---Mina Session.write流程探索.doc--zhengli的更多相关文章

  1. Mina Session

    Chapter 4 - Session The Session is at the heart of MINA : every time a client connects to the server ...

  2. day58_9_24多对多建表手动,form组件(判断类型),cookies和session

    一.多对多建表关系之手动添加. 1.全自动 像之前讲过的一样,我们可以通过manytomanyField的字段来建立多对多关系: class Book(models.Model): title = m ...

  3. 由SecureCRT引发的思考和学习

    由SecureCRT引发的思考和学习 http://mp.weixin.qq.com/s?__biz=MzAxOTAzMDEwMA==&mid=2652500597&idx=1& ...

  4. SQLAlchemy并发写入引发的思考

    背景 近期公司项目中加了一个积分机制,用户登录签到会获取登录积分,但会出现一种现象就是用户登录时会增加双倍积分,然后生成两个积分记录.此为问题  问题分析 项目采用微服务架构,下图为积分机制流程   ...

  5. class_copyIvarList方法获取实例变量问题引发的思考

    在runtime.h中,你可以通过其中的一个方法来获取实例变量,那就是class_copyIvarList方法,具体的实现如下: - (NSArray *)ivarArray:(Class)cls { ...

  6. 通过session的id号获取对应的session

    说说为什么要用session!!! 每次访问端通过普通http协议访问tomcat时,访问端包括网页或Android app等,tomcat都会自动生成一个不同的session,而且session的i ...

  7. 一个ScheduledExecutorService启动的Java线程无故挂掉引发的思考

    2018年12月12日18:44:53 一个ScheduledExecutorService启动的Java线程无故挂掉引发的思考 案件现场 不久前,在开发改造公司一个端到端监控日志系统的时候,出现了一 ...

  8. int.TryParse非预期执行引发的思考 ASP.NET -- WebForm -- 给图片添加水印标记 Windows -- 使用批处理文件.bat删除旧文件

    int.TryParse非预期执行引发的思考   问题出现 这天在写一个页面,想谨慎些就用了int.TryParse,结果出问题了. 代码如下: Copy int id = 1000; //Reque ...

  9. 由C# dynamic是否装箱引发的思考

    前言 前几天在技术群里看到有同学在讨论关于dynamic是否会存在装箱拆箱的问题,我当时第一想法是"会".至于为啥会有很多人有这种疑问,主要是因为觉得dynamic可能是因为有点特 ...

随机推荐

  1. WebRTC for android ios开发官方指南

    The WebRTC native code package can be found at: https://chromium.googlesource.com/external/webrtc ht ...

  2. Linux - 命令行 管道(Pipelines) 具体解释

    命令行 管道(Pipelines) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24249529 管道操作符" ...

  3. VI使用说明 (转)

    vi使用方法(ZT)         vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器,这里只是简单地介绍一下它的用法和一小部分指令.由于对Unix及Lin ...

  4. python classmethod方法 和 staticmethod

    classmethod() 是一个类方法,用来装饰对应的函数.被classmethod 装饰之后就无需实例化,也不需要在函数中传self,但是被装饰的函数第一个参数需要是cls来表示自身类.可以用来调 ...

  5. 高复用率的RTSPClient组件EasyRTSPClient调用说明

    EasyRTSPClient 调用说明 概述 EasyRtspClient是EasyDarwin家族中针对RTSP协议的拉流组件 EasyRtspClient视频支持H264.H265.MJPEG格式 ...

  6. runsv

    runsv(8) manual page http://smarden.org/runit/runsv.8.html Name runsv - starts and monitors a servic ...

  7. Optimistic concurrency control

    Optimistic concurrency control https://en.wikipedia.org/wiki/Optimistic_concurrency_control Optimist ...

  8. SP1437 Longest path in a tree(树的直径)

    应该是模板题了吧 定义: 树的直径是指一棵树上相距最远的两个点之间的距离. 方法:我使用的是比较常见的方法:两边dfs,第一遍从任意一个节点开始找出最远的节点x,第二遍从x开始做dfs找到最远节点的距 ...

  9. 关于Spring注解 @Service @Component @Controller @Repository 用法

    @Component 相当于实例化类的对象,其他三个注解可以理解为@Component的子注解或细化. 在annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个spr ...

  10. eclips 创建 maven项目

    Maven安装完成后我们就可以在Eclipse中新建自己的Maven项目了.我们可以在Eclipse中选择new一个project,在出现的对话框中选择Maven目录下的Maven Project. ...