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:

            The NIO transport is currently the most used. It provides a full asynchronous implementation of all I/O operations by using the selector-based approach that’s included in Java since Java 1.4 and the NIO subsystem.(NIO传输方式是用的最广泛的,通过基于Selector方式,它提供了一种全异步运行IO操作的实现。)

Real No-Blocking(非常重要的一段话):
       One feature that offers only the NIO transport at the moment is called “zero-file-copy”. This feature allows you to quickly and efficiently transfer content from your file system. The feature
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:

            The OIO transport is a compromise in Netty. It builds on the known unified API but isn’t asynchronous by nature because it uses the blocking java.net implementations under the hood.At first glance, this transport may not look useful to you,
but it has its use cases.
            When using these classes, you usually have one thread that handles the acceptance of new sockets (server-side) and then creates a new thread for each accepted connection to serve the traffic over the socket. This is needed as every I/O operation
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.
           Knowing that operations may block, you may start to wonder how Netty uses it while still providing the same way of building APIs. Here Netty makes use of the SO_TIMEOUT that you can set on a socket.
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:

             Netty also includes the embedded transport. This isn’t a real transport when you compare it with the others listed previously, but it’s included to complete this section. If it’s not a real transport, what can it be used for? The embedded
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(变速箱)的更多相关文章

  1. Netty源代码学习——EventLoopGroup原理:NioEventLoopGroup分析

    类结构图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd29ya2luZ19icmFpbg==/font/5a6L5L2T/fontsize/400/f ...

  2. Netty源代码学习——ChannelPipeline模型分析

    參考Netty API io.netty.channel.ChannelPipeline A list of ChannelHandlers which handles or intercepts i ...

  3. struts2源代码学习之初始化(一)

    看struts2源代码已有一段时日,从今天開始,就做一个总结吧. 首先,先看看怎么调试struts2源代码吧,主要是下面步骤: 使用Myeclipse创建一个webproject 导入struts2须 ...

  4. [Java] LinkedList / Queue - 源代码学习笔记

    简单地画了下 LinkedList 的继承关系,如下图.只是画了关注的部分,并不是完整的关系图.本博文涉及的是 Queue, Deque, LinkedList 的源代码阅读笔记.关于 List 接口 ...

  5. 开源中国安卓client源代码学习(一) 渐变启动界面

    开源中国安卓client源代码学习(一) 渐变启动界面 准备学习安卓开发, 看到网上有人推荐开源中国安卓client的源代码, 说里面包括了大部分技术, 于是准备好好研究研究. 特开通此系列博客来记录 ...

  6. 读Flask源代码学习Python--config原理

    读Flask源代码学习Python--config原理 个人学习笔记,水平有限.如果理解错误的地方,请大家指出来,谢谢!第一次写文章,发现好累--!. 起因   莫名其妙在第一份工作中使用了从来没有接 ...

  7. nginx源代码学习资源(不断更新)

    nginx源代码学习是一个痛苦又快乐的过程,以下列出了一些nginx的学习资源. 首先要做的当然是下载一份nginx源代码,能够从nginx官方站点下载一份最新的. 看了nginx源代码,发现这是一份 ...

  8. JDK源代码学习系列07----Stack

                                                                   JDK源代码学习系列07----Stack 1.Stack源代码很easy ...

  9. netty深入学习之一: 入门篇

    netty深入学习之一: 入门篇 本文代码下载: http://download.csdn.net/detail/cheungmine/8497549 1)Netty是什么 Netty是Java NI ...

随机推荐

  1. 大数据技术人年度盛事! BDTC 2016将于12月8-10日在京举行

    2016年12月8日-10日,由中国计算机学会(CCF)主办,CCF大数据专家委员会承办,中国科学院计算技术研究所和CSDN共同协办的2016中国大数据技术大会(Big Data Technology ...

  2. Android Activity与Service的交互方式

    参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...

  3. linux下安装busybox

    1.获取busybox源码并解压,这里使用天嵌提供的“busybox-1.16.0.tar.bz2” #tar xvf busybox-.tar.bz2 -C / 解压的目的地址实际上是:/opt/E ...

  4. 常用原生JS兼容性写法汇总

    1.添加事件方法 addHandler:function(element,type,handler){ if(element.addEventListener){//检测是否为DOM2级方法 elem ...

  5. 内存管理tcmalloc

    tcmalloc https://code.google.com/p/gperftools/

  6. 30+最佳Ajax jQuery的自动完成插件的例子

    在这篇文章中,我们将介绍35个jQuery AJAX的自动完成提示例子. jQuery 的自动完成功能,使用户快速找到并选择一定的价值.每个人都想要快速和即时搜索输入栏位,因为这个原因,许 流行的搜索 ...

  7. 【Java】Java里String 的equals和==

    Java里面有对象和对象的引用的概念,在String方面,==比较的是引用,equals比较的是对象的具体值. String s1 = new String("abc");Stri ...

  8. 【BZOJ1030】[JSOI2007]文本生成器

    [题意] 给定一些单词,我们定义一篇可读文章至少包含一个这样的单词,求长度为M的可读文章总数. [分析] 用前几题的方法可以求长度为M的不可读的文章总数Sum,所以我们可以用26^M-Sum来求出可读 ...

  9. mysql优化21条经验(转)

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序 员需要去关注的事情.当我们去设计数据库表结构,对操作数 ...

  10. 移动大数据时代最IN编程语言必读书单

    移动大数据时代最IN编程语言必读书单 这是一个快速更迭,快鱼吃慢鱼的时代.从IT 时代演变成 DT 时代,再到现在的智能时代.急速革新的各种新技术.新工具.新平台,需要程序员掌握良好的编程思想和学习方 ...