processor顾名思义,就是进行IO处理,处理当前session的数据读写,并进行业务处理。

在mina server初始化的时候,会初始化一个processor池,通过NioSocketAcceptor的构造器传入池的大小,默认是当前处理器的个数+1。

processor池里面有一个jdk提供的 线程池 - Executors.newCachedThreadPool()。各个processor线程会引用此线程池,即每个processor线程都在这个线程池里面运行。

在mina server实际处理时,每个processor相当于一个线程,轮流处理当前的session队列里面的数据(每个processor里面的session相当于顺序处理,共享一个线程)。

每个processor有一个Selector对象

processor类图

processor端的处理逻辑相对有点复杂,看下面的流程图。

1、把新添加进来的session注册到当前processor的Selector里面的read事件,并初始化session;
2、判断当前Selector是否有读写事件;
3、若第2步有读事件时,进入步骤4,若没有的话,直接到第6步;
4、处理当前读事件,并把处理后的数据放入到flush队列里面;
5、把第4步执行的结果flush到客户端;
6、处理session,比如session idle时间等。
7、重新执行第1步,循环执行。

processor类源码:

  1. private class Processor implements Runnable {
  2. public void run() {
  3. assert (processorRef.get() == this);
  4.  
  5. int nSessions = 0;
  6. lastIdleCheckTime = System.currentTimeMillis();
  7.  
  8. for (;;) {
  9. try {
  10. // This select has a timeout so that we can manage
  11. // idle session when we get out of the select every
  12. // second. (note : this is a hack to avoid creating
  13. // a dedicated thread).
  14. long t0 = System.currentTimeMillis();
  15. int selected = select(SELECT_TIMEOUT);
  16. long t1 = System.currentTimeMillis();
  17. long delta = (t1 - t0);
  18.  
  19. if ((selected == 0) && !wakeupCalled.get() && (delta < 100)) {
  20. // Last chance : the select() may have been
  21. // interrupted because we have had an closed channel.
  22. if (isBrokenConnection()) {
  23. LOG.warn("Broken connection");
  24.  
  25. // we can reselect immediately
  26. // set back the flag to false
  27. wakeupCalled.getAndSet(false);
  28.  
  29. continue;
  30. } else {
  31. LOG.warn("Create a new selector. Selected is 0, delta = " + (t1 - t0));
  32. // Ok, we are hit by the nasty epoll
  33. // spinning.
  34. // Basically, there is a race condition
  35. // which causes a closing file descriptor not to be
  36. // considered as available as a selected channel, but
  37. // it stopped the select. The next time we will
  38. // call select(), it will exit immediately for the same
  39. // reason, and do so forever, consuming 100%
  40. // CPU.
  41. // We have to destroy the selector, and
  42. // register all the socket on a new one.
  43. registerNewSelector();
  44. }
  45.  
  46. // Set back the flag to false
  47. wakeupCalled.getAndSet(false);
  48.  
  49. // and continue the loop
  50. continue;
  51. }
  52.  
  53. // Manage newly created session first
  54. nSessions += handleNewSessions();
  55.  
  56. updateTrafficMask();
  57.  
  58. // Now, if we have had some incoming or outgoing events,
  59. // deal with them
  60. if (selected > 0) {
  61. //LOG.debug("Processing ..."); // This log hurts one of the MDCFilter test...
  62. process();
  63. }
  64.  
  65. // Write the pending requests
  66. long currentTime = System.currentTimeMillis();
  67. flush(currentTime);
  68.  
  69. // And manage removed sessions
  70. nSessions -= removeSessions();
  71.  
  72. // Last, not least, send Idle events to the idle sessions
  73. notifyIdleSessions(currentTime);
  74.  
  75. // Get a chance to exit the infinite loop if there are no
  76. // more sessions on this Processor
  77. if (nSessions == 0) {
  78. processorRef.set(null);
  79.  
  80. if (newSessions.isEmpty() && isSelectorEmpty()) {
  81. // newSessions.add() precedes startupProcessor
  82. assert (processorRef.get() != this);
  83. break;
  84. }
  85.  
  86. assert (processorRef.get() != this);
  87.  
  88. if (!processorRef.compareAndSet(null, this)) {
  89. // startupProcessor won race, so must exit processor
  90. assert (processorRef.get() != this);
  91. break;
  92. }
  93.  
  94. assert (processorRef.get() == this);
  95. }
  96.  
  97. // Disconnect all sessions immediately if disposal has been
  98. // requested so that we exit this loop eventually.
  99. if (isDisposing()) {
  100. for (Iterator<S> i = allSessions(); i.hasNext();) {
  101. scheduleRemove(i.next());
  102. }
  103.  
  104. wakeup();
  105. }
  106. } catch (ClosedSelectorException cse) {
  107. // If the selector has been closed, we can exit the loop
  108. break;
  109. } catch (Throwable t) {
  110. ExceptionMonitor.getInstance().exceptionCaught(t);
  111.  
  112. try {
  113. Thread.sleep(1000);
  114. } catch (InterruptedException e1) {
  115. ExceptionMonitor.getInstance().exceptionCaught(e1);
  116. }
  117. }
  118. }
  119.  
  120. try {
  121. synchronized (disposalLock) {
  122. if (disposing) {
  123. doDispose();
  124. }
  125. }
  126. } catch (Throwable t) {
  127. ExceptionMonitor.getInstance().exceptionCaught(t);
  128. } finally {
  129. disposalFuture.setValue(true);
  130. }
  131. }
  132. }

mina2的processor的更多相关文章

  1. mina2

      远程通信 Mina2 学习笔记 作者:李少华 邮箱:xiaosanshaoli@126.com QQ:305409913 2010-12-23   初稿 引言... 1 一.       Mina ...

  2. 【原创】NIO框架入门(四):Android与MINA2、Netty4的跨平台UDP双向通信实战

    概述 本文演示的是一个Android客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo. 当前由于NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能. ...

  3. 【原创】NIO框架入门(三):iOS与MINA2、Netty4的跨平台UDP双向通信实战

    前言 本文将演示一个iOS客户端程序,通过UDP协议与两个典型的NIO框架服务端,实现跨平台双向通信的完整Demo.服务端将分别用MINA2和Netty4进行实现,而通信时服务端你只需选其一就行了.同 ...

  4. 【原创】NIO框架入门(二):服务端基于MINA2的UDP双向通信Demo演示

    前言 NIO框架的流行,使得开发大并发.高性能的互联网服务端成为可能.这其中最流行的无非就是MINA和Netty了,MINA目前的主要版本是MINA2.而Netty的主要版本是Netty3和Netty ...

  5. mina2线程详解

    1主要流程 read  in  data: IO读入(IoProcessor)---日志记录.解码.threadPool(IoFilter)---业务逻辑处理(IoHandler) write  ou ...

  6. 反射动态创建不同的Processor

    1. 定义抽象方法 public abstract class BaseProcesser    {        public abstract void GetCustomerReportCard ...

  7. processor, memory, I/O

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION 3.3 INTERCONNECTION S ...

  8. improve performance whilemaintaining the functionality of a simpler and more abstract model design of processor hardware

    Computer Systems A Programmer's Perspective Second Edition In this chapter, we take a brief look at ...

  9. instruction-set architecture Processor Architecture

    Computer Systems A Programmer's Perspective Second Edition We have seen that a processor must execut ...

随机推荐

  1. 四则运算V1.1

    作业:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/997 代码:https://coding.net/u/Dawnfox/p/f4 ...

  2. 为何linux(包括mac系统)执行指令要加上 ./ ??

    比如,现在要在$HIVE_HOME/bin下执行hive指令来启动hive,则该指令的执行顺序如下所示: 1 先找PATH路径 1.1 如果PATH路径下配置了$HIVE_HOME/bin,无论PAT ...

  3. (25)Django中操作cookie与session组件(添加cookie和删除cookie)

    cookie是存在于客户端浏览器上的键值对,是明文的 cookie是当用户访问网站时候和数据提起携带过去,安全性比较差, 容易被拦截 session存在于服务端的键值对,是一串加密的字符串 当用户登陆 ...

  4. 《DSP using MATLAB》Problem 5.12

    1.从别的地方找的证明过程: 2.代码 function x2 = circfold(x1, N) %% Circular folding using DFT %% ----------------- ...

  5. spring事务中出现oracle游标溢出的解决方案

    本例事务中大量查询SQL语句,会导致oracle游标溢出:对于数据库游标出现解决方案:1.大量查询SQL语句取消事务,只针对插入/更新 做事务处理2.用临时表代替大量查询SQL语句推荐使用第二种方案

  6. <--------------------------Java接口如何使用------------------------------>

    关键词:interface --->接口      implements--->实现 1接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的”类”. 接口只描述所应 ...

  7. gcc的编译属性和选项

    1.指定内存默认对其参数: __attribute__((packed)):按一字节对其__attribute__((aligned(n))):从此之后默认按n字节对其 例如: struct stu ...

  8. MySQL DataType--字符串类型

    ================================================= VARCHAR类型存储空间问题 当MySQL表使用ROW_FORMAT=FIXED时,对于定义VAR ...

  9. Django 之老师讲的教师,班级学生,class_2_teacher四个表格的项目

    现数据库中有四张表格 要求:实现查询功能  页面显示教师  id 姓名  教学班级 url 处设置 url(r'teacher_list/',teacher_list),  teacher_list函 ...

  10. HDU2220 Eddy's AC难题

    版权声明:长风原创 https://blog.csdn.net/u012846486/article/details/27853287 Eddy's AC难题 Time Limit: 3000/100 ...