Netty Study Record One

Source: Internet
Author: User

Recently in the study Netty related knowledge, think "Netty authoritative guide" This book is still very good, suitable for me this kind of beginner. In addition to many examples of Netty itself, it is very interesting to learn. Simply record,

The general server code is as follows:

 public void Run () throws Exception {Eventloopgroup bossgroup = new Nioeventloopgroup ();        Eventloopgroup Workergroup = new Nioeventloopgroup ();            try {serverbootstrap b = new Serverbootstrap (); B.group (Bossgroup, Workergroup). Channel (Nioserversocketchannel.class). Childhandler (New Channelin Itializer<socketchannel> () {@Override public void Initchannel (Socketchannel ch) throw                            S Exception {ch.pipeline (). AddLast (New Objectencoder (),                            New Objectdecoder (classresolvers.cachedisabled (NULL)), new Objectechoserverhandler (),                New Myserverhandler ());            }             });            Bind and start to accept incoming connections.                    B.bind (port). Sync (). Channel (). Closefuture (). sync (); } finally {Bossgroup.shutdowngracefulLy ();        Workergroup.shutdowngracefully (); }    }

Read the "Netty authoritative guide" This book, the following code should be relatively easy to understand, as its name, a call Bossgroup (boss), a call Workergroup (worker), Bossgroup used to accept the request thread Group, Workergroup is used to process the IO operation thread Group, Bossgroup is called after receiving the request Workergroup to handle,

Eventloopgroup Bossgroup = new Nioeventloopgroup ();
Eventloopgroup Workergroup = new Nioeventloopgroup ();


Serverbootstrap is a startup tool class, and the Childhandler () method is used to add operations classes that process the channel and initialize them. Channelpipeline is used for intra-chain management of channel processing classes.

B.bind (port). Sync (). Channel (). Closefuture (). sync (); This sentence is broken down into two sentences to understand the point:

B.bind (port). sync (); Thread synchronization blocking waits for the server to bind to the specified port.

... channel (). Closefuture (). Sync () After successfully binding to the port, add a pipe-closed listener to the channel and block synchronously until the channel is closed, and the thread will execute down and end the process.





Netty Study Record One

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.