Java NIO Framework Netty Tutorial (i) –hello Netty

Source: Internet
Author: User
Tags static class

If you don't know what Netty is, you can do something. That can be a simple search to find out. I can only say that Netty is a NIO framework that can be used to develop distributed Java programs. What can be done, you can try to play the imagination. Technology is to serve people and not to limit people.

If you already have everything, let's start with a piece of code. Programmers are accustomed to the first step, nature is "Hello World", but Netty official website example has abandoned "Hello World". Let's write the simplest example of "Hello world" to get started.

 1/** 2 * Netty Server Code 3 * 4 * @author lihzh 5 * @alia onecoder 6 * @blog http://www.coderli.com 7 */8 Pub Lic class HelloServer {9 public static void Main (String args[]) {one//Server service Initiator Serve                         Rbootstrap bootstrap = new Serverbootstrap (New Nioserversocketchannelfactory (14  Executors.newcachedthreadpool (), Executors.newcachedthreadpool ()); 16//Set up a class to handle client messages and various message events (Handler) bootstrap. Setpipelinefactory (New Channelpipel                             Inefactory () {@Override public channelpipeline getpipeline () 21 Throws Exception {return Channels. Pi  Peline (New Helloserverhandler ());  24} 25});  26//Open 8000-Port statement client access. Bootstrap.bind (New Inetsocketaddress (8000));         $32 33 for private static class Helloserverhandler extends /** 34 * When there is a client bound to the service side of the trigger, print "Hello world, I ' m server." * * * * @alia Onecoder * @author lihzh * * * @Override P              ublic void channelconnected (Channelhandlercontext ctx, channelstateevent e) {43  System.out.println ("Hello World, I ' m server.");   44} 45} 46}

/** * Netty Client code * * @author LIHZH * @alia onecoder * @blog http://www.coderli.com * * public class Helloclient { public static void Main (String args[]) {//Client service initiator Clientbootstrap bootstrap = new Clientboo                          Tstrap (New Nioclientsocketchannelfactory (Executors.newcachedthreadpool (),          Executors.newcachedthreadpool ())); Set up a Class (Handler) bootstrap.setpipelinefactory (new Channelpipelinefactory () {@Overri) that handles service-side messages and various message events De public channelpipeline Getpipeline () throws Exception {return Channels.pipeline (new Hell              Oclienthandler ());          }          });      Connect to the local 8000 port of the server Bootstrap.connect (new inetsocketaddress ("127.0.0.1", 8000)); } private static Class Helloclienthandler extends Simplechannelhandler {/** * When bound to the service side of the trigger, hit       Print "Hello world, I ' m client."   * * @alia Onecoder * @author Lihzh */@Override public void Channelconnec Ted (Channelhandlercontext ctx, channelstateevent e) {System.out.println ("Hello World, I ' m c   Lient. ");}}

 

Since it is distributed, it naturally needs to be divided into multiple services. In Netty, you need to differentiate between server and client services. All clients are bound to the server, and they cannot communicate directly through Netty. (other means of self-use, not included.) )。 In the vernacular. This communication process, server side open port, for client connection, client initiates the request, connects to server specified port, completes the binding. You can then communicate freely. is actually the ordinary socket connection communication process.

Netty Framework is based on the event mechanism, simply said, is what happens, find the relevant processing method. Just follow the fire to find 119, robbed for 1101 reasons. So, here, we're dealing with this event after the client and server have completed the connection. When the connection was completed, Netty knew that he had told me, and I was in charge of dealing with it. This is the role of the framework. Netty, provide a lot of events, will be slowly contact and introduction.

You should already be able to get started:)

Java NIO Framework Netty Tutorial (i) –hello Netty

Related Article

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.