Netty Getting started to master a

Source: Internet
Author: User

Because one job required to run from one area of Huawei to another, I finally met my great God (Li Linfeng:Netty China promoter, now Huawei Technology Co., Ltd. Platform middleware Architecture and Design Department designer), like the technology I can't wait to communicate with him, the great god of technology is really good, little brother worship, Daniel gave me to explain a lot of design ideas, I met most developers, most of the communication technology, with what technology, then feel very good, after and Daniel Exchanges, only to understand the design thought is very important, Many people pay attention to the use of God horse God horse frame, God horse God horse technology, but do not know the framework design ideas, why so design, this may be the difference between the designer and the Code farm (I also only dare to call himself a code farm (>﹏<. ),and the great God after the exchange feel that they are weak , a good product is the design of ideas and souls, OK, here we will not rip eggs.

      Well, tell me why I wrote this article, because I need to use netty Span style= "font-family: Song Body" >, just met some problems, it is I think of the great God, the great God is too busy waiting for a long time to return to my mail ( Huawei Internal Mail, Huawei does not allow Internet access ﹏ < &NBSP; ) after work on the Internet to find some information, found that most are not very full, or very general, so that people do not understand, read the e-book very painful bar, the leadership also arranged a different task, and believe that for beginners also encountered a lot of problems it, Then their own data collated a bit, write this article, I will not regularly update Netty articles, so to have patience, it is best to pay attention to my microblog, of course, impatient to eat hot tofu , God horse technical problems can also be exchanged, I csdn blog name jimmy_zjf888, feel write good to a praise, send flowers, of course, the poor do not spit groove throw stool just fine.

First of all we need to know God horse is Netty, why should we use netty,netty advantage is God horse?

Netty is an asynchronous communication framework based on the JAVA NIO class library , with its architectural features: asynchronous nonblocking, event-driven, high performance, high reliability, and high customization , then NIO and Traditional IO What is the difference between God and horse? Let's take a look at the code here , the client and server here I use the company's front desk and visit customers to do examples .

/**

* Traditional Socket server

 * @author -jimmy_zjf888-

*

*/

Public class  Ordinaryserver {

Public Static void Main (string[] args)throws Exception {

Create a socket service, open and listen on port 8888

ServerSocket server=New serversocket (8888);

while (true) {

Get a socket (block)

Final Socket socket = server.accept ();

System. out. println ("a new customer! ");

Front desk sister to guests tea pour water

Handler (socket);

}

}

/**

* Read Data

 * @param Socket

 * @throws Exception

*/

Public static void handler (socket socket) {

Try {

byte [] bytes = new byte[1024];

Get an input stream for a socket

InputStream InputStream = Socket.getinputstream ();

while (true) {

Loop read read data (blocking)

int read = inputstream.read (bytes);

if (Read! =-1) {

System. out. println (new String (bytes, 0, read));

}Else{

break;

}

}

} catch (Exception e) {

E.printstacktrace ();

}finally{

Try {

Socket.close ();

} catch (IOException e) {

E.printstacktrace ();

}

}

}

}

Let's run a code like this, turn on debug mode


Oh, well, at first, I found the final socket socket = server.accept (); When the code executes to this sentence, it starts to block, so let's use the console's Telnet to simulate it.


, and then enter, find the code down, ouch found int read = inputstream.read (bytes); Execution to this sentence is blocked


A summary of the traditional IO has 2 blocking points of a system server.accept () blocking the time, the other point is

Nputstream.read (bytes) When the obstruction, if so blocking down, if there are other guests to our company to visit, is not the front desk sister can not take care of the guest? Yes, we'll try to telnet again.



When we found a new guest coming, our front desk was only ill-cared for, because she was busy taking care of her first guest, so what should we do? Do you have to recruit a few more front-desk sister Ah? Heard here a little excitement, and recruit sister, to see how to achieve, first we need a front desk administrator, when the guests can be responsible for allocating the vacant front desk to take care of the guests, the code is as follows ...

Public Static void Main (string[] args)throws Exception {

Create a pool of threads

Executorservice Newcachedthreadpool = executors. Newcachedthreadpool ();

Create a socket service, open and listen on port 8888

ServerSocket server=New serversocket (8888);

while (true) {

Get a socket (block)

Final Socket socket = server.accept ();

System. out. println ("a new customer! ");

Delivery of the vacant front desk sister to receive guests

Newcachedthreadpool.execute (new Runnable () {

@Override

Public void run () {

Front desk sister to guests tea pour water

Handler (socket);

}

});

}

}

Run the code to see if we can solve the problem as we imagined.



We'll do it again, see if the problem will still exist.



We summarize, the single-threaded case can only have a client, multi-threaded case may have multiple clients, but very consumption of memory (to recruit a lot of front sister, the company's resources are limited, can not keep inviting the front desk, although for our program ape is welfare, but for the company is a small open capital, So what should we do? Take a look at the next article)

Netty Getting started to master a

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.