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.
-
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.
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.
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.
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.
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.
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.
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.
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:
-
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
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.
talk to us to improve this guide.