Netty源代码学习——Included transports(变速箱)
Transport API核心:
Channel介面
类图表示Channel含有Pipeline和Config接口,pipeline上一节有所介绍。
Channel是线程安全的,这表示在多线环境下操作同一个Channel。不会有数据问题。
final Channel channel = null;
final ByteBuf buf = Unpooled.copiedBuffer("your data",
CharsetUtil.UTF_8); // #1
Runnable writer = new Runnable() { // #2
@Override
public void run() {
channel.write(buf.duplicate());
}
};
Executor executor = Executors.newCachedThreadPool(); // #3
// write in one thread
executor.execute(writer); // #4
// write in another thread
executor.execute(writer); // #5 #1 Create ByteBuf that holds data to write
#2 Create Runnable which writes data to channel
#3 Obtain reference to the Executor which uses threads to execute tasks
#4 Hand over write task to executor for execution in thread
#5 Hand over another write task to executor for execution in thread
This method guarantees that the messages are written in the same order as you passed them to the write method.(这种方法保证信息被写入到Channel中的顺序和调用write方法的顺序是一致的。)
Transports:
每一个传输方式都有相应的EventLoop:
最后一个ThreadPerChannelEventLoop就是OIO(Old-IO)的事件监听器。
NIO – Nonblocking I/O:
provides a way to transfer the bytes from the file system to the networkstack without copying the bytes from the kernel space to the user space. Be aware that not all operation systems support this. Please refer to operating system’s documentation to find
out if it’s supported. Also, be aware that you’ll only be able to benefit from this if you don’t use any encryption/compression of the data. Otherwise it will need to copy the bytes first to the user space to do the actual work, so only transferring the raw
content of a file makes use of this feature. What actually would work is to „pre-encrypt“ a file before transfer it.One application that can really make use of this is an FTP or HTTP server that downloads big files.The next transport I’ll discuss is the OIO
transport, which provides a blocking transport.
大概意思:“zero-file-copy”是只提供给NIO传输方式使用的特性。这个特性同意你以非常快而且高效的方式从文件系统之中来传输内容。这个特性把本地文件里的字节内容能够不通过从内核空间拷贝到用户空间的情况下通过网络传输出去。
请注意并非全部的操作系统支持这一特性。
请參考详细的操作系统文档来查看这个特性的实现情况。相同请注意假设要使用这一特性,请不要把源数据进行不论什么的编解码操作。否则的话会把源数据从内核空间拷贝到用户空间里去做编解码操作,所以唯独发送源数据才会用到这一特性。更实际点的就是把源数据事先以加密的形式保存。
基于FTP或者HTTP服务的用于下载大文件的应用程序能够从这个特性中获益。
OIO – Old blocking I/O:
but it has its use cases.
on the socket may block at any time. If you share the same thread over more than one connection (socket), this could lead to a situation where blocking an operation could block all other sockets from doing their
work.
This timeout specifies the maximum number of milliseconds to wait for an I/O operation to complete. If the operation doesn’t complete within the specified timeout, a SocketTimeoutException is thrown. Netty catches this SocketTimeoutException and moves on with
its work. Then on the next EventLoop run, it tries again. Unfortunately, this is the only way to do this and still confirm the inner working of Netty. The problem with this approach is that firing the SocketTimeoutException isn’t free, as it needs to fill
the StrackTrace, and so on.
Local – In VM transport :
Netty contains the so-called local transport. This transport implementation can be used to communicate within a VM and still use the same API you’re used to. The transport is fully asynchronous as NIO .
Embedded transport:
transport allows you to interact with your different ChannelHandler implementation more easily. It’s also easy to embed ChannelHandler instances in other ChannelHandlers and use them like a helper class.This is often used in test cases to test a specific ChannelHandler
implementation, but can also be used to re-use some ChannelHandler in your own ChannelHandler without event extend it. For this purpose, it comes with a concrete Channel implementation.
版权声明:本文博主原创文章,博客,未经同意不得转载。
Netty源代码学习——Included transports(变速箱)的更多相关文章
- Netty源代码学习——EventLoopGroup原理:NioEventLoopGroup分析
类结构图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29ya2luZ19icmFpbg==/font/5a6L5L2T/fontsize/400/f ...
- Netty源代码学习——ChannelPipeline模型分析
參考Netty API io.netty.channel.ChannelPipeline A list of ChannelHandlers which handles or intercepts i ...
- struts2源代码学习之初始化(一)
看struts2源代码已有一段时日,从今天開始,就做一个总结吧. 首先,先看看怎么调试struts2源代码吧,主要是下面步骤: 使用Myeclipse创建一个webproject 导入struts2须 ...
- [Java] LinkedList / Queue - 源代码学习笔记
简单地画了下 LinkedList 的继承关系,如下图.只是画了关注的部分,并不是完整的关系图.本博文涉及的是 Queue, Deque, LinkedList 的源代码阅读笔记.关于 List 接口 ...
- 开源中国安卓client源代码学习(一) 渐变启动界面
开源中国安卓client源代码学习(一) 渐变启动界面 准备学习安卓开发, 看到网上有人推荐开源中国安卓client的源代码, 说里面包括了大部分技术, 于是准备好好研究研究. 特开通此系列博客来记录 ...
- 读Flask源代码学习Python--config原理
读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因 莫名其妙在第一份工作中使用了从来没有接 ...
- nginx源代码学习资源(不断更新)
nginx源代码学习是一个痛苦又快乐的过程,以下列出了一些nginx的学习资源. 首先要做的当然是下载一份nginx源代码,能够从nginx官方站点下载一份最新的. 看了nginx源代码,发现这是一份 ...
- JDK源代码学习系列07----Stack
JDK源代码学习系列07----Stack 1.Stack源代码很easy ...
- netty深入学习之一: 入门篇
netty深入学习之一: 入门篇 本文代码下载: http://download.csdn.net/detail/cheungmine/8497549 1)Netty是什么 Netty是Java NI ...
随机推荐
- js更新页面,随机更新数字
代码1: <script> function getRandom(){ var i = Math.random()*40+160; document.getElementById(&quo ...
- 简单安装python的pip工具模块
下载最新pip安装包 https://pypi.python.org/pypi/pip#downloads 安装 .tar.gz cd pip-.tar.gz python setup.py inst ...
- mybaits不能出现小于号
org.xml.sax.SAXParseException; lineNumber: 146; columnNumber: 54; The content of elements must consi ...
- ARM920T系统总线时序分析
一.系统总线时序图 二.分析 第一个时钟周期开始,系统地址总线给出需要访问的存储空间地址. 经过Tacs时间后,片选信号也相应给出,并且锁存当前地址线上地址信息. 再经过Tcso时间后,处理器给出当前 ...
- [CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
题目链接:D. A and B and Interesting Substrings 题目大意 给定26个小写字母的权值,一共26个整数(有正有负). 给定一个小写字母组成的字符串(长度10^5),求 ...
- MATLAB中的结构数组
MATLAB中的结构数组 结构数组: 结构是包含一组记录的数据类型,而记录则是存储在相应的字段中.结构的字段可以是任意一种MATLAB数据类型的变量或者对象.结构类型的变量也可以是一维的.二维的或多维 ...
- Codeforces Round #207 (Div. 2)
A:超级大水题: 代码: #include<cstdio> #define maxn 105 using namespace std; int n,a[maxn],x,y,ans; int ...
- 【HDU3440】House Man (差分约束)
题目: Description In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop ...
- lc面试准备:Invert Binary Tree
1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...
- 利用no_merge优化
SQL> select a.unit3_code 机构编码, 2 a.unit3_name 机构名称, 3 a.dept1_code 部门编码, 4 a.dept1_name 部门名称, 5 a ...