没有合适的资源?快使用搜索试试~ 我知道了~
netty 新NIO框架 文档
3星 · 超过75%的资源 需积分: 9 39 下载量 156 浏览量
2010-09-21
18:32:15
上传
评论
收藏 645KB PDF 举报
温馨提示
试读
25页
netty 新NIO框架 文档,在jetty项目中用到了该框架。 netty更注重IO读写,效率很高。更支持servlet1.0异步的websocket。值得一看
资源推荐
资源详情
资源评论
The Netty Project 3.2 User Guide
The Proven Approach
to Rapid Network
Application Development
3.2.1.Final, r2319
ii
Preface .............................................................................................................................. iii
1. The Problem ........................................................................................................... iii
2. The Solution ........................................................................................................... iii
1. Getting Started ............................................................................................................... 1
1.1. Before Getting Started ............................................................................................ 1
1.2. Writing a Discard Server ......................................................................................... 1
1.3. Looking into the Received Data ................................................................................ 3
1.4. Writing an Echo Server ........................................................................................... 4
1.5. Writing a Time Server ............................................................................................ 5
1.6. Writing a Time Client ............................................................................................. 7
1.7. Dealing with a Stream-based Transport ...................................................................... 8
1.7.1. One Small Caveat of Socket Buffer ................................................................. 8
1.7.2. The First Solution ........................................................................................ 9
1.7.3. The Second Solution ................................................................................... 10
1.8. Speaking in POJO instead of ChannelBuffer .............................................................. 12
1.9. Shutting Down Your Application ............................................................................. 14
1.10. Summary ........................................................................................................... 17
2. Architectural Overview .................................................................................................. 18
2.1. Rich Buffer Data Structure ..................................................................................... 18
2.2. Universal Asynchronous I/O API ............................................................................ 18
2.3. Event Model based on the Interceptor Chain Pattern ................................................... 19
2.4. Advanced Components for More Rapid Development .................................................. 20
2.4.1. Codec framework ....................................................................................... 20
2.4.2. SSL / TLS Support .................................................................................... 21
2.4.3. HTTP Implementation ................................................................................. 21
2.4.4. Google Protocol Buffer Integration ................................................................ 21
2.5. Summary ............................................................................................................ 21
iii
Preface
1. The Problem
Nowadays we use general purpose applications or libraries to communicate with each other. For example,
we often use an HTTP client library to retrieve information from a web server and to invoke a remote
procedure call via web services.
However, a general purpose protocol or its implementation sometimes does not scale very well. It is like
we don't use a general purpose HTTP server to exchange huge files, e-mail messages, and near-realtime
messages such as financial information and multiplayer game data. What's required is a highly optimized
protocol implementation which is dedicated to a special purpose. For example, you might want to implement
an HTTP server which is optimized for AJAX-based chat application, media streaming, or large file transfer.
You could even want to design and implement a whole new protocol which is precisely tailored to your need.
Another inevitable case is when you have to deal with a legacy proprietary protocol to ensure the
interoperability with an old system. What matters in this case is how quickly we can implement that protocol
while not sacrificing the stability and performance of the resulting application.
2. The Solution
The Netty project is an effort to provide an asynchronous event-driven network application framework and
tooling for the rapid development of maintainable high-performance high-scalability protocol servers and
clients.
In other words, Netty is a NIO client server framework which enables quick and easy development of
network applications such as protocol servers and clients. It greatly simplifies and streamlines network
programming such as TCP and UDP socket server development.
'Quick and easy' does not mean that a resulting application will suffer from a maintainability or a
performance issue. Netty has been designed carefully with the experiences earned from the implementation
of a lot of protocols such as FTP, SMTP, HTTP, and various binary and text-based legacy protocols. As
a result, Netty has succeeded to find a way to achieve ease of development, performance, stability, and
flexibility without a compromise.
Some users might already have found other network application framework that claims to have the same
advantage, and you might want to ask what makes Netty so different from them. The answer is the
philosophy where it is built on. Netty is designed to give you the most comfortable experience both in terms
of the API and the implementation from the day one. It is not something tangible but you will realize that
this philosophy will make your life much easier as you read this guide and play with Netty.
Chapter 1.
1
Getting Started
This chapter tours around the core constructs of Netty with simple examples to let you get started quickly.
You will be able to write a client and a server on top of Netty right away when you are at the end of this
chapter.
If you prefer top-down approach in learning something, you might want to start from Chapter 2,
Architectural Overview and get back here.
1.1. Before Getting Started
The minimum requirements to run the examples which are introduced in this chapter are only two; the latest
version of Netty and JDK 1.5 or above. The latest version of Netty is available in the project download
page. To download the right version of JDK, please refer to your preferred JDK vendor's web site.
As you read, you might have more questions about the classes introduced in this chapter. Please refer to the
API reference whenever you want to know more about them. All class names in this document are linked
to the online API reference for your convenience. Also, please don't hesitate to contact the Netty project
community and let us know if there's any incorrect information, errors in grammar and typo, and if you
have a good idea to improve the documentation.
1.2. Writing a Discard Server
The most simplistic protocol in the world is not 'Hello, World!' but DISCARD. It's a protocol which discards
any received data without any response.
To implement the DISCARD protocol, the only thing you need to do is to ignore all received data. Let us
start straight from the handler implementation, which handles I/O events generated by Netty.
package org.jboss.netty.example.discard;
public class DiscardServerHandler extends SimpleChannelHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
e.getCause().printStackTrace();
Channel ch = e.getChannel();
ch.close();
}
}
Writing a Discard Server
2
DiscardServerHandler extends SimpleChannelHandler, which is an implementation
of ChannelHandler. SimpleChannelHandler provides various event handler methods that
you can override. For now, it is just enough to extend SimpleChannelHandler rather than to
implement the handler interfaces by yourself.
We override the messageReceived event handler method here. This method is called with a
MessageEvent, which contains the received data, whenever new data is received from a client. In
this example, we ignore the received data by doing nothing to implement the DISCARD protocol.
exceptionCaught event handler method is called with an ExceptionEvent when an exception
was raised by Netty due to I/O error or by a handler implementation due to the exception thrown while
processing events. In most cases, the caught exception should be logged and its associated channel
should be closed here, although the implementation of this method can be different depending on what
you want to do to deal with an exceptional situation. For example, you might want to send a response
message with an error code before closing the connection.
So far so good. We have implemented the first half of the DISCARD server. What's left now is to write the
main method which starts the server with the DiscardServerHandler.
package org.jboss.netty.example.discard;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
public class DiscardServer {
public static void main(String[] args) throws Exception {
ChannelFactory factory =
new NioServerSocketChannelFactory (
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
ServerBootstrap bootstrap = new ServerBootstrap (factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
return Channels.pipeline(new DiscardServerHandler());
}
});
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
bootstrap.bind(new InetSocketAddress(8080));
}
}
剩余24页未读,继续阅读
资源评论
- bopzhou2013-02-25非常的好啊~~就是看的有点吃力
- a4096939322012-08-20英文的啊啊!
Funzhou
- 粉丝: 8
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功