Java NIO Framework Netty tutorial (i) –hello Netty

Source: Internet
Author: User

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 service-side code3  *  4  * @authorLihzh5 * @alia Onecoder6 * @bloghttp://www.coderli.com 7  */  8  public classHelloServer {9   Ten      public Static voidmain (String Args[]) { one         //Server service Initiator aServerbootstrap bootstrap =NewServerbootstrap ( -                 NewNioserversocketchannelfactory ( - Executors.newcachedthreadpool (), the Executors.newcachedthreadpool ()));  -         //set up a class to handle client messages and various message events (Handler) - Bootstrap -. Setpipelinefactory (Newchannelpipelinefactory () { + @Override -                      publicchannelpipeline getpipeline () +                             throwsException { a                         returnChannels at. Pipeline (NewHelloserverhandler ());  -                     }   -                 });  -         //Open 8000-port Confession client Access.  -Bootstrap.bind (NewInetsocketaddress (8000));  -     }   in    -     Private Static classHelloserverhandlerextends   to Simplechannelhandler { +    -         /**  the * When there is a client bound to the server, trigger, print "Hello world, I ' m server." *          *   $ * @alia OnecoderPanax Notoginseng          * @authorLihzh -          */   the @Override +          public voidchannelconnected ( a Channelhandlercontext ctx, the channelstateevent E) {   +System.out.println ("Hello world, I ' m Server."));  -         }   $     }   $}

/*** Netty Client code * *@authorLihzh * @alia onecoder * @bloghttp://www.coderli.com  */   public classhelloclient { public Static voidmain (String Args[]) {//Client Service LauncherClientbootstrap bootstrap =NewClientbootstrap (Newnioclientsocketchannelfactory (executors.newcachedthreadpool (), Exe          Cutors.newcachedthreadpool ())); //set up a class that handles Server-side messages and various message events (Handler)Bootstrap.setpipelinefactory (Newchannelpipelinefactory () {@Override publicChannelpipeline Getpipeline ()throwsException {returnChannels.pipeline (NewHelloclienthandler ());          }          }); //connect to the server on the local 8000 portBootstrap.connect (Newinetsocketaddress ("127.0.0.1", 8000)); }        Private Static classHelloclienthandlerextendsSimplechannelhandler {/*** triggered when binding to the server, print "Hello world, I ' m client." * * @alia Onecoder *@authorLihzh*/@Override public voidchannelconnected (channelhandlercontext ctx, channelstateevent e) {System.out.println ( /c7>"Hello world, I ' m client."); }      }  }  

 

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.