A lot of people recently asked me if I had a Netty4 Hello World example, I knew that Netty was going to be 4, and I knew only 4 of the package name was completely on the side, because Netty was independent from JBoss, and adopted the new Netty.io domain name, but did not expect the code has such a large adjustment.
Since promised others, take time to look at Netty4, also by the way to add their knowledge. Let's start with the simplest Hello world. The following code is based on the most recent version of Netty4,netty4.0.12-final. Because Netty4 most of the code is subcontracting, a lot of engineering, there may be trouble for your development, but Netty4 also provides a netty4-all jar package, which contains all the code, so you can rely on development. The jar package is used here for Onecoder.
View Sourceprint?01.
/**
02.
* Netty4 服务端代码
03.
*
04.
* @author lihzh
05.
* @date 2013年11月15日 下午1:10:06
06.
* @website http://www.coderli.com
07.
*/
08.
public
class
HelloWorldServer {
09.
10.
public
static
void
main(String[] args) {
11.
// EventLoop 代替原来的 ChannelFactory
12.
EventLoopGroup bossGroup =
new
NioEventLoopGroup();
13.
EventLoopGroup workerGroup =
new
NioEventLoopGroup();
14.
try
{
15.
ServerBootstrap serverBootstrap =
new
ServerBootstrap();
16.
// server端采用简洁的连写方式,client端才用分段普通写法。
17.
serverBootstrap.group(bossGroup, workerGroup)
18.
.channel(NioServerSocketChannel.
class
)
19.
.childHandler(
new
ChannelInitializer<SocketChannel>() {
20.
@Override
21.
public
void
initChannel(SocketChannel ch)
22.
throws
Exception {
23.
ch.pipeline().addLast(
new
HelloServerHandler());
24.
}
25.
}).option(ChannelOption. SO_KEEPALIVE ,
true
);
26.
27.
ChannelFuture f = serverBootstrap.bind(
8000
).sync();
28.
f.channel().closeFuture().sync();
29.
}
catch
(InterruptedException e) {
30.
}
finally
{
31.
workerGroup.shutdownGracefully();
32.
bossGroup.shutdownGracefully();
33.
}
34.
}
35.
36.
private
static
class
HelloServerHandler
extends
37.
ChannelInboundHandlerAdapter {
38.
39.
/**
40.
* 当绑定到服务端的时候触发,打印"Hello world, I‘m client."
41.
*
42.
* @alia OneCoder
43.
* @author lihzh
44.
* @date 2013年11月16日 上午12:50:47
45.
*/
46.
@Override
47.
public
void
channelActive(ChannelHandlerContext ctx)
throws
Exception {
48.
System. out .println(
"Hello world, I‘m server."
);
49.
}
50.
}
51.
52.
}
53.
54.
/**
55.
* Netty4 客户端代码
56.
* @author OneCoder
57.
* @date 2013年11月15日 下午1:28:21
58.
* @website http://www.coderli.com
59.
*/
60.
public
class
HelloWorldClient {
61.
62.
public
static
void
main(String args[]) {
63.
// Client服务启动器 3.x的ClientBootstrap 改为Bootstrap,且构造函数变化很大,这里用无参构造。
64.
Bootstrap bootstrap =
new
Bootstrap();
65.
// 指定channel类型
66.
bootstrap.channel(NioSocketChannel.
class
);
67.
// 指定Handler
68.
bootstrap.handler(
new
HelloClientHandler());
69.
// 指定EventLoopGroup
70.
bootstrap.group(
new
NioEventLoopGroup());
71.
// 连接到本地的8000端口的服务端
72.
bootstrap.connect(
new
InetSocketAddress(
"127.0.0.1"
,
8000
));
73.
}
74.
75.
private
static
class
HelloClientHandler
extends
76.
ChannelInboundHandlerAdapter {
77.
78.
/**
79.
* 当绑定到服务端的时候触发,打印"Hello world, I‘m client."
80.
*
81.
* @alia OneCoder
82.
* @author lihzh
83.
* @date 2013年11月16日 上午12:50:47
84.
*/
85.
@Override
86.
public
void
channelActive(ChannelHandlerContext ctx)
throws
Exception {
87.
System. out .println(
"Hello world, I‘m client."
);
88.
}
89.
}
90.
}
Some of the major changes and comparisons are commented in the code. A few simple additions:
Nioeventloopgroup is a multithreaded event loop that handles I/O operations. That is the thread pool in Netty4, in 3.x, a channel is created by ChannelFactory, and the newly created channel is automatically registered to a hidden I/O thread. 4.0 replaces the ChannelFactory with the new interface named Eventloopgroup, which is composed of one or more eventloop. A new channel is not automatically registered to Eventloopgroup, but the user can explicitly call Eventloopgroup.register () to register. In the server-side bootstrap parameter, there are two eventloopgroup, the first commonly referred to as ' Boss ', to receive incoming connection requests. The second is called a ' worker ', which handles the information in the connection that the boss accepts and registers with the worker.
Channelinitializer is a special handler for easy configuration of user-defined handler implementations, as shown in the code. The user-replicated Initchannel (C-ch) method is triggered during the lifetime of the channelregistered, and is then removed from the channelpipeline after the call.