Mina 学习纪录

MINA 是基于 filterchain 来做的。类似web请求的filter,通过一串串的filter 处理费业务性的处理逻辑,如编解码,日志等。然后交由 handler 去实施自己的业务处理逻辑。
在Mina里面,对于NIO的 socket实现有三类I/O 工作线程:

一:Acceptor thread   它接受连接请求,然后将连接转由 I/O processor thread 来做读写操作。
     每一个 SocketAcceptor 创建一个 acceptor thread,这个 acceptor thread 的线程数是不可配置的。

二:Commector thread  它是尝试与远程节点进行连接,并将建立的连接转由 I/O processor thread 来做读写操作。
     每一个 SocketConnect 创建一个 Commector thread,这个 Commector thread 的线程数也是不可配置的。

三: I/O processor thread 它执行实际的读写操作,直到连接关闭。
      每一个 SocketAcceptor 或者 SocketConnect  创建自己的 I/O processor thread 。这个 I/O processor thread 的数目视可以配置的,默认的数据是 CPU核数+1。这也就是每一个IoService可以获得的I/O processor thread 数量。

EG:
SocketAcceptor acceptor = new SocketAcceptor(Runtime.getRuntime().availableProcessors() + 1, Executors.newCachedThreadPool());
这就是创建了一个 acceptor thread 和 cpus+1 数量的 I/O Processor。
对于 SocketConnector的创建方式也是如此 :
SocketConnector connector = new SocketConnector(Runtime.getRuntime().availableProcessors() + 1, Executors.newCachedThreadPool());

 

摘一张MINA 官方ppt 的图片说明下它的工作模式。

 

 

I/O Service 在mina里面就是 SocketAcceptor/SocketConnector ,他们做实际的 IO操作。

 

下面是要使用过程中的注意事项: 

Do's !!!
• Follow the KISS principle
• Keep the chain short
• Do not use an executor if not needed
• Tune the number of IoProcessors
• Use only one codec filter
• If you have a problem, then your
codec/handler probably sucks...

 

DON'Ts !!!

Don't use the logging filter. Use Log4j.

Your filter must be threadsafe

Don't expect that you will receive data in one single block

Don't forget about the negative impact Nagle's algorithm has on performance

Don't use Direct buffers unless absolutely needed...

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值