Java NIO Framework Netty Tutorial (12) Concurrent access test (middle)

Source: Internet
Author: User

Write in front: the test and resolution of Netty concurrency problem is completely beyond my expectations, more and more things to say. So this medium, that is to say, there will be the next article. As for the discovery of the problem point, Onecoder is also trying to verify.

Continue the concurrency problem. In the Java NIO Framework Netty Tutorial (11) Concurrent access test (above), we are actually testing a pseudo-concurrency scenario. The bottom layer actually has only one channel in operation, and there is no question of what is not responding. and the actual concurrency is not the meaning of the independent client, there will naturally be independent channel. That is, a server is likely to build relationships with a lot of clients, there are a lot of channel, many times the binding. Let's simulate this scenario.

View Sourceprint? 01. /** 02. * Netty并发,多connection,socket并发测试 03. * 04. * @author lihzh(OneCoder) 05. * @alia OneCoder 06. * @Blog http://www.coderli.com 07. * @date 2012-8-13 下午10:47:28 08. */ 09. public  class  ConcurrencyNettyMultiConnection { 10.  11. public  static  void  main(String args[]) { 12. final  ClientBootstrap bootstrap =  new  ClientBootstrap( 13. new  NioClientSocketChannelFactory( 14. Executors.newCachedThreadPool(), 15. Executors.newCachedThreadPool())); 16. // 设置一个处理服务端消息和各种消息事件的类(Handler) 17. bootstrap.setPipelineFactory( new  ChannelPipelineFactory() { 18. @Override 19. public  ChannelPipeline getPipeline()  throws  Exception { 20. return  Channels.pipeline( new  ObjectEncoder(), 21. new  ObjectClientHandler() 22. ); 23. } 24. }); 25. for  ( int  i =  0 ; i <  3 ; i++) { 26. bootstrap.connect( new  InetSocketAddress( "127.0.0.1" 8000 )); 27. } 28. } 29. }

Seeing this piece of code, you may wonder what this is doing. He is a direct validation based on the Java selector, and the Netty is encapsulated on top of it. In fact, Onecoder also did selector layer verification.

View Sourceprint? 01. /** 02. * Selector打开端口数量测试 03. * 04. * @author lihzh 05. * @alia OneCoder 06. * @blog http://www.coderli.com 07. */ 08. public  class  ConcurrencySelectorOpen { 09.  10. /** 11. * @author lihzh 12. * @alia OneCoder 13. * @throws IOException 14. * @throws InterruptedException 15. * @date 2012-8-15 下午1:57:56 16. */ 17. public  static  void  main(String[] args)  throws  IOException, InterruptedException { 18. for  ( int  i =  0 ; i <  3 ; i++) { 19. Selector selector = Selector.open(); 20. SocketChannel channel = SocketChannel.open(); 21. channel.configureBlocking( false ); 22. channel.connect( new  InetSocketAddress( "127.0.0.1" 8000 )); 23. channel.register(selector, SelectionKey.OP_READ); 24. } 25. Thread.sleep( 300000 ); 26. } 27. }

Similarly, using the Process Explorer to observe the port occupancy, starts to look like the cool Shell elder brother's observed effect. When you do not start the server side, that is, do not actually establish a link with the server side, 3 times selector Open, occupies 6 ports.


When the server is started, the binding takes up 9 ports


In fact, more than three, is the actual binding of the 8000 port. The rest is the cool shell Big Brother said the internal loop link. In other words, for our actual scenario, the number of ports required for a link is 3. The number of ports and resources of the operating system is of course limited, which means that when we increase the link, the error will inevitably occur. Onecoder try the scene 3,000 times, and there is no error, but it is certainly necessary to go wrong.

And look at the server,


This is also directly through the selector connection when the server, in addition to two internal loopback ports, all through the listening 8000 port to communicate with the outside world, so there is no port limit service side problem. However, it can also be seen that if the link is not controlled, the server also maintains a large number of connections consuming system resources! So, good coding is how important.

Back to our theme, Netty the scene. Use the same scenario as the selector test, single-threaded, one-bootstrap, and multiple times connect to see the error and port usage. The code is the code provided at the beginning.

Look at the test results,


The same connection still takes up 9 ports. If the Channle.close () method is called manually, the port that is linked to 8000 is freed, that is, 6 ports are occupied.

Increase the number of consecutive connections to 10000. Www.it165.net

First, there is no error, in each close channel case, the client port occupancy (server-side situation is similar).


Visible, and did not grow as infinitely as selector. This is the great role of the workers in Netty and helps us manage these trivial links. Specific analysis, we leave the next chapter, you can also study it yourself. (Onecoder Note: If you do not close the channel, you will find that the occupancy of Port 8000 will increase rapidly.) The system will be very jammed. )

Adjust the test code to simulate a client request with a thread.

View Sourceprint? 01. /** 02. * Netty并发,多connection,socket并发测试 03. * 04. * @author lihzh(OneCoder) 05. * @alia OneCoder 06. * @Blog http://www.coderli.com 07. * @date 2012-8-13 下午10:47:28 08. */ 09. public  class  ConcurrencyNettyMultiConnection { 10.  11. public  static  void  main(String args[]) { 12. final  ClientBootstrap bootstrap =  new  ClientBootstrap( 13. new  NioClientSocketChannelFactory( 14. Executors.newCachedThreadPool(), 15. Executors.newCachedThreadPool())); 16. // 设置一个处理服务端消息和各种消息事件的类(Handler) 17. bootstrap.setPipelineFactory( new  ChannelPipelineFactory() { 18. @Override 19. public  ChannelPipeline getPipeline()  throws  Exception { 20. return  Channels.pipeline( new  ObjectEncoder(), 21. new  ObjectClientHandler() 22. ); 23. } 24. }); 25. for  ( int  i =  0 ; i <  1000 ; i++) { 26. // 连接到本地的8000端口的服务端 27. Thread t =  new  Thread( new  Runnable() { 28. @Override 29. public  void  run() { 30. bootstrap.connect( new  InetSocketAddress( "127.0.0.1" 8000 )); 31. try  { 32. Thread.sleep( 300000 ); 33. catch  (InterruptedException e) { 34. e.printStackTrace(); 35. } 36. } 37. }); 38. t.start(); 39. } 40. } 41. }

Unsurprisingly, the problem with a friend who asked me questions on Weibo should be the same:

write:973
write:974
August 17, 2012 9:57:28 pm Org.jboss.netty.channel.SimpleChannelHandler
Warning: EXCEPTION, implement One.coder.netty.chapter.eight.ObjectClientHandler.exceptionCaught () for proper Handling.
Java.net.ConnectException:Connection Refused:no Further information

This problem, the author has not yet analyzed the reason, only from the port occupancy situation, with the previous test case run out basically similar effect. It should be explained that the port is not enough to cause, but onecoder still dare not jump to conclusions. After the study, we Tell:) you have any ideas, can also be provided to me, I come to verify.

Java NIO Framework Netty Tutorial (12) Concurrent access test (middle)

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.