µ±Ç°Î»ÖÃ: > Linux·þÎñÆ÷ > Tomcat >

Tomcatѧϰ֮Acceptor

ʱ¼ä:2014-12-30 13:28À´Ô´:linux.it.net.cn ×÷Õß:IT

¼ò½é

Acceptor¹ËÃû˼Òå¾ÍÊǽÓÊÕÆ÷£¬ÓÃÓÚ½ÓÊÕÓû§ÇëÇó£¬Õâ½ÚÖ÷ÒªÊÇ·ÖÎöAcceptorµÄÆô¶¯ºÍ´¦ÀíÇëÇó£¡

Ê×ÏÈÀ´¿´AcceptorµÄÀàͼ

´ÓͼÖпÉÒÔ¿´³öAcceptorʵÏÖÁËRunnable½Ó¿Ú£¬¿ÉÒÔ×÷Ϊһ¸öÏß³ÌÆô¶¯£¬ÇÒ¶¼ÊÇEndpointµÄÄÚ²¿Àà¡£AcceptorÓÐ3ÖÖʵÏÖJIo¡¢Apr¡¢Nio±¾½Ú²»»á½éÉÜËûÃÇÖ®¼äµÄÇø±ð£¬³£ÓõÄÊÇJIoEndpoint.Acceptor£¬ºóÃæµÄÀý×Ó£¬Èç¹ûûÓÐ×öÌرð˵Ã÷Ö¸µÄÊÇJIoEndpointÀàÀïÃæµÄ½ÓÊÕÆ÷¡£

 

Æô¶¯

ÔÚtomcatÆô¶¯µÄʱºò»áÆô¶¯Endpoint,»áµ÷ÓÃËüµÄstartInternal·½·¨£¬À´¿´¿´Õâ¸ö·½·¨×öÁËÄÄЩÊ£º

 

[java] view plaincopyprint?
 
  1. public void startInternal() throws Exception {  
  2.   
  3.     if (!running) {  
  4.         running = true;  
  5.         paused = false;  
  6.   
  7.         // Create worker collection  
  8.         if (getExecutor() == null) {  
  9.             createExecutor();  
  10.         }  
  11.   
  12.         initializeConnectionLatch();  
  13.   
  14.         startAcceptorThreads();  
  15.   
  16.         // Start async timeout thread  
  17.         Thread timeoutThread = new Thread(new AsyncTimeout(),  
  18.                 getName() + "-AsyncTimeout");  
  19.         timeoutThread.setPriority(threadPriority);  
  20.         timeoutThread.setDaemon(true);  
  21.         timeoutThread.start();  
  22.     }  
  23. }  
1¡¢³õʼ»¯Ò»¸ö±ÕËø£¬Õâ¸ö±ÕËøÓÃÓÚ¿ØÖÆÁ¬½ÓµÄ£¬Ã¿ÊÍ·ÅÒ»¸öÁ¬½Ó£¬±ÕËø¾Íµ÷ÓÃcountDown·½·¨Ò»´Î¡£getMaxConnections·½·¨·µ»Ø10000£¬Ò²¾ÍÊÇĬÈÏÁ¬½ÓÊýΪ10000

 

[java] view plaincopyprint?
 
  1. protected LimitLatch initializeConnectionLatch() {  
  2.     if (maxConnections==-1return null;  
  3.     if (connectionLimitLatch==null) {  
  4.         connectionLimitLatch = new LimitLatch(getMaxConnections());  
  5.     }  
  6.     return connectionLimitLatch;  
  7. }  

2¡¢Æô¶¯´øÓнÓÊÕÆ÷µÄÏß³Ì

 

[java] view plaincopyprint?
  1. protected final void startAcceptorThreads() {  
  2.     int count = getAcceptorThreadCount();  
  3.     acceptors = new Acceptor[count];  
  4.   
  5.     for (int i = 0; i < count; i++) {  
  6.         acceptors[i] = createAcceptor();  
  7.         Thread t = new Thread(acceptors[i], getName() + "-Acceptor-" + i);  
  8.         t.setPriority(getAcceptorThreadPriority());  
  9.         t.setDaemon(getDaemon());  
  10.         t.start();  
  11.     }  
  12. }  

(1)getAcceptorThreadCount£ºÔÚendpoint³õʼ»¯Ê±£¬µ÷ÓÃAbstractEndpoint.init·½·¨£¬½ô½Ó×ŵ÷ÓÃbind·½·¨£¬ÊµÏÖÊÇÔÚ×ÓÀàʵÏֵģ¬¿´¿´JIOEndpointÀàµÄ

bind·½·¨

[java] view plaincopyprint?
 
  1.   public void bind() throws Exception {  
  2.   
  3.         // Initialize thread count defaults for acceptor  
  4.         if (acceptorThreadCount == 0) {  
  5.             acceptorThreadCount = 1;  
  6.         }  
  7. ......  
  8. }  
ËùÒÔÔÚ»ùÓÚJIOµÄʵÏÖ·½Ê½getAcceptorThreadCountĬÈÏΪ1£¬Ò²¾ÍÊÇĬÈÏÖ»ÓÐÒ»¸öÏß³ÌÉÏÓнÓÊÕÆ÷

 

(2)È»ºó´´½¨count¸öỊ̈߳¬Ã¿¸öÏß³ÌÉ϶¼´øÓÐÒ»¸ö½ÓÊÕÆ÷£¬²¢°ÑÕâЩÏß³ÌÉèÖÃΪºǫ́Ị̈߳¬×îºóÆô¶¯ÕâЩÏß³Ì

 

´¦ÀíÇëÇó

ÏÂÃæ¿´¿´Æô¶¯ÕâЩÏ̺߳ó£¬ÕâЩÏß³Ì×öÁËÄÄЩÊ£¿

ÓÉÓÚorg.apache.tomcat.util.net.AbstractEndpoint.AcceptorʵÏÖÁËrunnable½Ó¿Ú£¬Æô¶¯ÕâЩỊ̈߳¬Ò²¾ÍÊÇÔËÐÐ

org.apache.tomcat.util.net.AbstractEndpoint.AcceptorʵÏÖÀàµÄrun·½·¨£¬»¹ÊÇÒÔJIOµÄʵÏÖ·½Ê½ÎªÀýÀ´¿´£¬ÒÔÏ´úÂëÊ¡ÂÔÁ˺ܶàÒì³£´¦Àí

 

[java] view plaincopyprint?
 
  1. public void run() {  
  2.     while (running) {  
  3.         while (paused && running) {  
  4.             Thread.sleep(50);  
  5.         }  
  6.   
  7.                 countUpOrAwaitConnection();  
  8.   
  9.         socket = serverSocketFactory.acceptSocket(serverSocket);  
  10.   
  11.         if (running && !paused && setSocketOptions(socket)) {  
  12.             if (!processSocket(socket)) {  
  13.                 countDownConnection();  
  14.                ......  
  15.             }  
  16.         } else {  
  17.             countDownConnection();  
  18.         }  
  19.     }   
  20. }  

1¡¢ÕûÌåÀ´¿´ÊÇÒ»¸öwhileÑ­»·£¬Ö»Òª·þÎñÆ÷ûÓб»¹Ø±Õ£¬Õâ¸öwhileÑ­»·»áÒ»Ö±ÔËÐУ¬µ±Ã»ÓÐÇëÇóµ½À´Ê±£¬ÕâЩÏ߳̾ͻásleep

2¡¢»ñÈ¡Ò»¸ö½ÓÁ¬£¬µ×²ãµ÷ÓõÄÊÇAQSµÄsync.acquireSharedInterruptibly(1)·½·¨£¬ÓпÉÄÜÐèÒª×èÈû

3¡¢Èç¹ûÓÐÇëÇóµ½À´¾Í»áµ÷ÓÃserverSocketFactory.acceptSocket·½·¨£¬¸Ã·½·¨²»Ï¸½²£¬µ×²ãµ÷ÓõľÍÊÇsocket.accept()·½·¨

4¡¢·½·¨µ÷Óóɹ¦¾Í»á·ÖÏíÒ»¸öÏß³ÌÀ´´¦ÀíÕâ´ÎÇëÇ󣬴¦ÀíÇëÇóµÄ´úÂëÔÚprocessSocket·½·¨ÖÐÍê³É£¬ºóÃæ»áϸ½²Õâ¸ö·½·¨

5¡¢Èç¹ûÔÚ´¦ÀíµÄ¹ý³ÌÖлòÕßÔÚacceptSocketµÄ¹ý³ÌÖÐÓÐÒì³£¶¼»áµ÷ÓÃcountDownConnection·½·¨¼õÉÙÁ¬½ÓÊý£¬Èç¹ûsocket»¹´ò¿ª¾Í¹Ø±Õsocket

 

²Î¿¼×ÊÁÏ

TomcatÐÔÄܵ÷У֮Á¬½ÓÆ÷Ä£¿éJIO¡¢APRºÍNIO

(ÔðÈα༭£ºIT)
------·Ö¸ôÏß----------------------------
À¸Ä¿Áбí
ÍƼöÄÚÈÝ