tomcat源码研读笔记—tomcat的启动之七 HttpConnector的启动

我们从启动流程图上可以了解到,当standardWrapper启动完成之后,那么返回到standardService的start方法里边时,会执行HttpConnector的start方法:


HttpConnector的start方法源码:

public void start() throwsLifecycleException {

 

       // Validate and update our current state

       if (started)

           throw new LifecycleException

               (sm.getString("httpConnector.alreadyStarted"));

       threadName = "HttpConnector[" + port + "]";

       lifecycle.fireLifecycleEvent(START_EVENT, null);

       started = true;

 

       // Start our background thread

       threadStart();

 

       // Create the specified minimum number of processors

       while (curProcessors < minProcessors) {

           if ((maxProcessors > 0) && (curProcessors >=maxProcessors))

               break;

           HttpProcessor processor = newProcessor();

           recycle(processor);

       }

 

}

 

因HttpConnector实现了Runnable接口,十一这个了出现threadStart:

private voidthreadStart() {

 

       log(sm.getString("httpConnector.starting"));

 

        thread = new Thread(this, threadName);

        thread.setDaemon(true);

        thread.start();

 

    }

 

调用了线程的run方法:

public voidrun() {

        // Loop until we receive a shutdowncommand

        while (!stopped) {

            // Accept the next incomingconnection from the server socket

            Socket socket = null;

            try {

                //                if (debug >= 3)

                //                    log("run: Waiting onserverSocket.accept()");

                socket = serverSocket.accept();

                //                if (debug >= 3)

                //                    log("run: Returnedfrom serverSocket.accept()");

                if (connectionTimeout > 0)

                   socket.setSoTimeout(connectionTimeout);

               socket.setTcpNoDelay(tcpNoDelay);

            } catch (AccessControlExceptionace) {

                log("socket acceptsecurity exception", ace);

                continue;

            } catch (IOException e) {

                //                if (debug >= 3)

                //                    log("run: Acceptreturned IOException", e);

                try {

                    // If reopening fails, exit

                    synchronized (threadSync) {

                        if (started &&!stopped)

                            log("accepterror: ", e);

                        if (!stopped) {

                            //                    if (debug >= 3)

                            //                        log("run: Closing server socket");

                           serverSocket.close();

                            //                        if (debug >= 3)

                            //                            log("run:Reopening server socket");

                            serverSocket = open();

                        }

                    }

                    //                    if (debug >= 3)

                    //                        log("run:IOException processing completed");

                } catch (IOException ioe) {

                    log("socket reopen, ioproblem: ", ioe);

                    break;

                } catch (KeyStoreException kse){

                    log("socket reopen,keystore problem: ", kse);

                    break;

                } catch(NoSuchAlgorithmException nsae) {

                    log("socket reopen,keystore algorithm problem: ", nsae);

                    break;

                } catch (CertificateExceptionce) {

                    log("socket reopen,certificate problem: ", ce);

                    break;

                } catch(UnrecoverableKeyException uke) {

                    log("socket reopen,unrecoverable key: ", uke);

                    break;

                } catch (KeyManagementException kme) {

                    log("socket reopen,key management problem: ", kme);

                    break;

                }

 

                continue;

            }

 

            // Hand this socket off to anappropriate processor

            HttpProcessor processor =createProcessor();

            if (processor == null) {

                try {

                   log(sm.getString("httpConnector.noProcessor"));

                    socket.close();

                } catch (IOException e) {

                    ;

                }

                continue;

            }

            //            if (debug >= 3)

            //                log("run: Assigning socketto processor " + processor);

            processor.assign(socket);

 

            // The processor will recycleitself when it finishes

 

        }

 

        // Notify the threadStop() method thatwe have shut ourselves down

        //        if (debug >= 3)

        //            log("run: NotifyingthreadStop() that we have shut down");

        synchronized (threadSync) {

            threadSync.notifyAll();

        }

 

    }

 

从源码上我们可以知道这里采用了一个while循环,知道stop指令下达之后才会停止,否则一直循环等待,直到serverSocket.accept接收到请求之后,程序才会往下执行!我们才会开始下一章节的请求

HttpConnector的启动的关系类图:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值