netty Architectural Overview --reference
reference from:http://docs.jboss.org/netty/3.1/guide/html/architecture.html
In this chapter, we will examine what core functionalities are provided in Netty and how they constitute a complete network application development stack on top of the core. Please keep this diagram in mind as you read this chapter.
2.1. Rich Buffer Data Structure
Netty uses its own buffer API instead of NIO ByteBuffer to represent a sequence of bytes. This approach has significant advantage over using ByteBuffer. Netty's new buffer type, ChannelBuffer has been designed from ground up to address the problems of ByteBuffer and to meet the daily needs of network application developers. To list a few cool features:
You can define your buffer type if necessary.
Transparent zero copy is achieved by built-in composite buffer type.
A dynamic buffer type is provided out-of-the-box, whose capacity is expanded on demand, just like
StringBuffer.There's no need to call
flip()anymore.It is often faster than
ByteBuffer.
For more information, please refer to the org.jboss.netty.buffer package description.
2.2. Universal Asynchronous I/O API
Traditional I/O APIs in Java provided different types and methods for different transport types. For example,java.net.Socket and java.net.DatagramSocket do not have any common super type and therefore they have very different ways to perform socket I/O.
This mismatch makes porting a network application from one transport to the other tedious and difficult. The lack of portability between transports becomes a problem when you need to support more transports not rewriting the network layer of the application. Logically, many protocols can run on more than one transport such as TCP/IP, UDP/IP, SCTP, and serial port communication.
To make the matter worse, Java New I/O (NIO) API introduced the incompatibility with the old blocking I/O (OIO) API, and so will NIO.2 (AIO). Because all these APIs are different from each other in design and performance characteristics, you are often forced to determine which API your application will depend on before you even begin the implementation phase.
For instance, you might want to start with OIO because the number of clients you are going to serve will be very small and writing a socket server using OIO is much easier than using NIO. However, you are going to be in trouble when your business grows up exponentially and your server starts to serve tens of thousand clients simultaneously. You could start with NIO, but it might take much longer time to implement due to the complexity of the NIO Selector API, hindering rapid development.
Netty has a universal asynchronous I/O interface called Channel, which abstracts away all operations required to point-to-point communication. That is, once you wrote your application on one Netty transport, your application can run on other Netty transports. Netty provides a number of essential transports via one universal API:
NIO-based TCP/IP transport (See
org.jboss.netty.channel.socket.nio),OIO-based TCP/IP transport (See
org.jboss.netty.channel.socket.oio),OIO-based UDP/IP transport, and
Local transport (See
org.jboss.netty.channel.local).
Switching from one transport from the other usually takes just a couple lines of changes such as choosing a different ChannelFactory implementation.
Also, you are even able to take advantage of a new transport which is not written yet, serial port communication transport for instance, again by replacing just a couple lines of constructor calls. Moreover, you can write your own transport by extending the core API because it is highly extensible.
2.3. Event Model based on the Interceptor Chain Pattern
Well-defined and extensible event model is a must for an event-driven application. Netty does have a well-defined event model focused on I/O. It also allows you to implement your own event type without breaking the existing code at all because each event type is distinguished from each other by strict type hierarchy. This is another differentiator against other frameworks. Many NIO frameworks have no or very limited notion of event model; they often break the existing code when you try to add a new custom event type, or just do not allow extension.
A ChannelEvent is handled by a list of ChannelHandlers in a ChannelPipeline. The pipeline implements an advanced form of the Intercepting Filter pattern to give a user full control over how an event is handled and how the handlers in the pipeline interact with each other. For example, you can define what to do when a data is read from a socket:
public class MyReadHandler implementsSimpleChannelHandler{
public void messageReceived(ChannelHandlerContextctx,MessageEventevt) {
Object message = evt.getMessage();
// Do something with the received message.
... // And forward the event to the next handler.
ctx.sendUpstream(evt);
}
}
You can also define what to do when other handler requested a write operation:
public class MyWriteHandler implementsSimpleChannelHandler{
public void writeRequested(ChannelHandlerContextctx,MessageEventevt) {
Object message = evt.getMessage();
// Do something with the message to be written.
... // And forward the event to the next handler.
ctx.sendDownstream(evt);
}
}
For more information about the event model, please refer to the API documentation of ChannelEvent andChannelPipeline.
2.4. Advanced Components for More Rapid Development
On top of the core components mentioned above, that already enable the implementation of all types of network applications, Netty provides a set of advanced features to accelerate the development pace even more.
2.4.1. Codec framework
As demonstrated in Section 1.8, “ Speaking in POJO instead of ChannelBuffer ”, it is always a good idea to separate a protocol codec from a business logic. However, there are some complications when implementing this idea from scratch. You have to deal with the fragmentation of messages. Some protocols are multi-layered (i.e. built on top of other lower level protocol). Some are too complicated to be implemented in a single state machine.
Consequently, a good network application framework should provide an extensible, reusable, unit-testable, and multi-layered codec framework that generates maintainable user codec.
Netty provides a number of basic and advanced codecs built on top of its core to address most issues you will encounter when you write a protocol codec regardless if it is simple or not, binary or text - simply whatever.
2.4.2. SSL / TLS Support
Unlike old blocking I/O, it is a non-trivial task to support SSL in NIO. You can't simply wrap a stream to encrypt or decrypt data but you have to use javax.net.ssl.SSLEngine. SSLEngine is a state machine which is as complex as SSL is. You have to manage all possible states such as cipher suite and encryption key negotiation (or re-negotiation), certificate exchange and validation. Moreover, SSLEngine is not even completely thread-safe unlike usual expectation.
In Netty, SslHandler takes care of all the gory details and pitfalls of SSLEngine. All you need to do is to configure and insert the SslHandler to your ChannelPipeline. It also allows you to implement advanced features likeStartTLS very easily.
2.4.3. HTTP Implementation
HTTP is definitely the most popular protocol in the Internet. There are already a number of HTTP implementations such as a Servlet container. Then why does Netty have HTTP on top of its core?
Netty's HTTP support is very different from the existing HTTP libraries. It gives you complete control over how HTTP messages are exchanged in a low level. Because it is basically the combination of HTTP codec and HTTP message classes, there is no restriction such as enforced thread model. That is, you can write your own HTTP client or server that works exactly the way you want. You have full control over thread model, connection life cycle, chunked encoding, and as much as what HTTP specification allows you to do.
Thanks to its highly customizable nature, you can write a very efficient HTTP server such as:
Chat server that requires persistent connections and server push technology (e.g. Comet)
Media streaming server that needs to keep the connection open until the whole media is streamed (e.g. 2 hours of movie)
File server that allows the upload of large files without memory pressure (e.g. uploading 1GB per request)
Scalable mash-up client that connects to tens of thousand 3rd party web services asynchronously
2.4.4. Google Protocol Buffer Integration
Google Protocol Buffers are an ideal solution for the rapid implementation of a highly efficient binary protocol that evolves over time. With ProtobufEncoder and ProtobufDecoder, you can turn the message classes generated by Google Protocol Buffers Compiler (protoc) into Netty codec. Please take a look into the 'LocalTime' examplethat shows how easily you can create a high-performing binary protocol client and server from the sample protocol definition.
2.5. Summary
In this chapter, we reviewed the overall architecture of Netty from the feature-wise standpoint. Netty has simple yet powerful architecture. It is composed of three components - buffer, channel, and event model - and all advanced features are built on top of the three core components. Once you understood how these three work together, it should not be difficult to understand more advanced features which were covered briefly in this chapter.
You might still have an unanswered question about what the overall architecture looks exactly like and how each feature work together. If so, it is a good idea to talk to us to improve this guide.
netty Architectural Overview --reference的更多相关文章
- CHAPTER 1 Architectural Overview of Oracle Database 11g
Which SGA structures are required, and which are optional? The database buffer cache, log buffer, an ...
- netty Getting Started--reference
reference from:http://docs.jboss.org/netty/3.1/guide/html/start.html 1.1. Before Getting Started 1.2 ...
- User guide for Netty 4.x
Table of Contents Preface The Solution Getting Started Before Getting Started Writing a Discard Serv ...
- Java Netty 4.x 用户指南
问题 今天,我们使用通用的应用程序或者类库来实现互相通讯,比如,我们经常使用一个 HTTP 客户端库来从 web 服务器上获取信息,或者通过 web 服务来执行一个远程的调用. 然而,有时候一个通用的 ...
- 《Netty in action》 读书笔记
声明:这篇文章是记录读书过程中的知识点,并加以归纳总结,成文.文中图片.代码出自<Netty in action>. 1. 为什么用Netty? 每个框架的流行,都一定有它出众的地方.Ne ...
- Java Reference & ReferenceQueue一览
Overview The java.lang.ref package provides more flexible types of references than are otherwise ava ...
- Reference counted objects
Reference counted objects · netty/netty Wiki https://github.com/netty/netty/wiki/Reference-counted-o ...
- WSL quick overview
简介 WSL,是Windows Subsystem for Linux的缩写,字面意义上理解就是WIndows下的Linux子系统.WSL 由Microsoft Windows内核团队创建,目前如果最 ...
- Netty4.0 用户指南
原文链接http://netty.io/wiki/user-guide-for-4.x.html 前言 Nowadays we use general purpose applications or ...
随机推荐
- SQL Server中时间段查询和数据类型转换
不知道什么时候对数据独有情种,也许是因为所学专业的缘故,也许是在多年的工作中的亲身经历,无数据,很多事情干不了,数据精度不够,也很多事情干不了,有一次跟一个朋友开玩笑说,如果在写论文的时候,能有一份独 ...
- Python解决codeforces ---- 1
第一题 1A A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input stan ...
- Android list刷新后仍然定位到原来的位置,解决。
问题: 有一个list,点击item时会做一些事情,然后重新加载数据,此时希望点击重新刷新后item还在原来的位置,而不是跳转到开头. 实现如下: 1.listview添加监听setOnScrollL ...
- wzplayer for android V1.6.1 (支持音视频加密播放)
1.更新 2013-11-25: 1.6.1 修复1.6.0版本对rk版本的支持. 以往版本: 1.6.0 1)1.6.0修改了所有默认音频渲染使用AudioTrack输出,这样只要不播放视频,能支持 ...
- QQ输入法个人设置
常用 按键 外观 词库 账户 高级
- 机器学习&深度学习经典资料汇总,data.gov.uk大量公开数据
<Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...
- SharePoint 2010 WSP包部署过程中究竟发生什么?
转:http://www.xue163.com/158/6/1585365.html 在SharePoint 2010中,我们可以使用Visual Studio 2010轻松创建WSP包来安装Web ...
- jQuery里面的datepicker日期控件默认是显示英文的,如何显示中文或其他语言呢?
jQuery里面的datepicker日期控件默认是显示英文的,如何让他显示中文或其他呢? [官方的写法]: (1)引入JS文件: <script type="text/javascr ...
- ruby编程语言-学习笔记3(第4章 表达式和操作符)
4.6 操作符 了解优先级很重要 位移操作符 (0b1011)<< 1 # ==> "10110" 11 << 1 = 22 ...
- 用Eclipse和GDB构建ARM交叉编译和在线调试环境
我们在 Linux 主机中搭建我们的开发环境,使用 Ubuntu 10.04 LTS 为例. 搭建应用开发环境 安装 JRE Eclipse 依赖于Java 环境,所以必须先安装 JRE 或 JD ...