Netty Overview
What is Netty?
Netty是一个用于快速开发可维护的高性能协议服务器和客户端的异步的事件驱动网络应用框架。
Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.

Main Components
Bootstrap and ServerBootstrap
EventLoop
Channel
ChannelFuture
ChannelPipeline
ChannelHandlers
ChannelHandlerContext
Bootstrapping a server

Bootstrapping a client

Bootstrapping clients from a Channel

Bootstrap, EventLoop and Channel
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(workerGroup).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new SimpleServerHandler());
}
}).option(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
* Channel
BIO 是面向流的 IO 系统(InputStream / OutputStream),系统一次一个字节(Byte)地处理数据;NIO 是面向块的 IO 系统(Channel),系统通过缓冲区(ByteBuf)批量地处理数据。
This chapter requires login to view full content. You are viewing a preview.
Login to View Full Content