Java NIO Framework Netty Tutorial (17) Netty4 Hello World

Source: Internet
Author: User
Tags io domain io domain name

  • 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 lihzh05.* @date 2013年11月15日 下午1:10:0606.* @website http://www.coderli.com07.*/08.public class HelloWorldServer {09. 10.public static void main(String[] args) {11.// EventLoop 代替原来的 ChannelFactory12.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.@Override21.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 extends37.ChannelInboundHandlerAdapter {38. 39./**40.* 当绑定到服务端的时候触发,打印"Hello world, I‘m client."41.*42.* @alia OneCoder43.* @author lihzh44.* @date 2013年11月16日 上午12:50:4745.*/46.@Override47.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 OneCoder57.* @date 2013年11月15日 下午1:28:2158.* @website http://www.coderli.com59.*/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.// 指定Handler68.bootstrap.handler( new HelloClientHandler());69.// 指定EventLoopGroup70.bootstrap.group( new NioEventLoopGroup());71.// 连接到本地的8000端口的服务端72.bootstrap.connect( new InetSocketAddress("127.0.0.1" 8000));73.}74. 75.private static class HelloClientHandler extends76.ChannelInboundHandlerAdapter {77. 78./**79.* 当绑定到服务端的时候触发,打印"Hello world, I‘m client."80.*81.* @alia OneCoder82.* @author lihzh83.* @date 2013年11月16日 上午12:50:4784.*/85.@Override86.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.

Java NIO Framework Netty Tutorial (17) Netty4 Hello World

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.