博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Netty in Action笔记(Chapter 4)Transport
阅读量:6762 次
发布时间:2019-06-26

本文共 3632 字,大约阅读时间需要 12 分钟。

One of the most important tasks of a network application is transferring data. This can be done differently depending on the kind of transport used, but what gets transferred is always the same: bytes over the wire. Transports help abstract how the data is transferred. All you need to know is that you have bytes to send and receive. Nothing more, nothing less.

clipboard.png

The ChannelConfig has the entire configuration settings stored for the channel and allows for updating them on the fly. Additionally, you can also modify the ChannelPipeline on the fly, which allows you to add/remove ChannelHandler instances whenever needed. This can be used to build highly flexible applications with Netty.

clipboard.png

如何注册Listener:

clipboard.png

Please note that Channel is thread-safe, which means it is safe to operate on it from different Threads. All it s methods are safe to use in a multi-thread enviroment. Because of this it s safe to store a reference to it in your application and use it once the need arises to write something to the remote peer, even when using many threads.

clipboard.png

NIO Transport的特性:

Because of the nature of this transport, it may come with a bit of latency when processing events, and so can have a lower throughput than the OIO transport. This is caused by the way the selector works, as it may take some time to be notified about state changes. I m not talking about seconds of delay, only milliseconds. This may not sound like much of a delay, but it can add up if you try to use your network application in a network where gigabit speed is offered.
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 network stack without copying the bytes from the kernel space to the user space.

OIO Transport的特性:

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.

Embedded Transport的特性:

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.

【P65】

4.4 When to use each type of transport

clipboard.png

转载地址:http://qwdeo.baihongyu.com/

你可能感兴趣的文章
微服务技术栈
查看>>
NPOI workbook.RemoveSheetAt(0); 删除sheet页 次序 sheettmpRequire.CopySheet("Require", true);...
查看>>
Go标准库:深入剖析Go template
查看>>
ant design pro (四)新增页面
查看>>
uni - 使用npm
查看>>
ASP.NET Core多语言 (转载)
查看>>
java中比较两个double类型值的大小
查看>>
golang ----gc问题
查看>>
WPF去除边框的方法
查看>>
浅析NTFS 文件系统数据流安全问题
查看>>
Smart Device Framework 2.2 发布了
查看>>
Humble Numbers soj1029
查看>>
程序员技术练级攻略
查看>>
ls只显示文件名/只显示文件夹名
查看>>
Java并发编程:同步容器
查看>>
水晶报表之动态列--简化版实现
查看>>
验证控件的使用四( RangeValidator)
查看>>
网络编程(一):用C#下载网络文件的2种方法
查看>>
复制graphic
查看>>
基于NopCommerce的开源电商系统改造总结
查看>>