tomcat源代碼系列--請求處理過程

這裏寫圖片描述
前面已經分析完了Tomcat的啓動和關閉過程,本篇就來接着分析一下Tomcat中請求的處理過程。

在開始本文之前,咋們首先來看看一個Http請求處理的過程,一般情況下是 瀏覽器發送http請求->建立Socket連接->通過Socket讀取數據->根據http協議解析數據->調用後臺服務完成響應 ,詳細的流程圖如上圖所示,等讀者讀完本篇,應該就清楚了上圖所表達的意思。Tomcat既是一個HttpServer也是一個Servlet 容器,那麼這裏必然也涉及到如上過程,首先根據HTTP協議規範解析請求數據,然後將請求轉發給Servlet進行處理,因此順應這樣的思路,本文也將從 Http協議請求解析 , 請求如何轉發給Servlet 兩個方面來進行分析。首先來看Http協議請求解析。

Http協議請求解析

在 Tomcat啓動過程(Tomcat源代碼閱讀系列之三) 一文中,我們已經知道Tomcat啓動以後,默認情況下會通過 org.apache.tomcat.util.net.JIoEndpoint.Acceptor 監聽Socket連接,當監聽到有Socket連接的時候,就會調用 org.apache.tomcat.util.net.JIoEndpoint#processSocket 方法進行處理,下面我們就來看看此方法的代碼,爲了節省版面,只保留與本文相關的代碼。

org.apache.tomcat.util.net.JIoEndpoint#processSocket

protected boolean processSocket(Socket socket) {
        // Process the request from this socket
        try {
            SocketWrapper<Socket> wrapper = new SocketWrapper<Socket>(socket);
            wrapper.setKeepAliveLeft(getMaxKeepAliveRequests());
            // During shutdown, executor may be null - avoid NPE
            if (!running) {
                return false;
            }
            getExecutor().execute(new SocketProcessor(wrapper));
        } catch (RejectedExecutionException x) {
         //exception handler ...
         return false;
        }
        return true;
}

通過上面的代碼,我們可以看出首先將Socket封裝爲SocketWrapper,然後通過SocketProcessor來進行處理,因爲Tomcat必然面對用戶併發請求,因此這裏Socket的處理通過新的線程池來處理。接下來我們再來看看SocketProcess的代碼,同樣省略了一些非核心的代碼,代碼如下:

org.apache.tomcat.util.net.JIoEndpoint.SocketProcessor#run

public void run() {
        boolean launch = false;
        synchronized (socket) {
            try {
                SocketState state = SocketState.OPEN;

                try {
                    // SSL handshake
                    serverSocketFactory.handshake(socket.getSocket());
                } catch (Throwable t) {
                    ExceptionUtils.handleThrowable(t);
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString("endpoint.err.handshake"), t);
                    }
                    // Tell to close the socket
                    state = SocketState.CLOSED;
                }

                if ((state != SocketState.CLOSED)) {
                    if (status == null) {
                        // 1 
                        state = handler.process(socket, SocketStatus.OPEN);
                    } else {
                        state = handler.process(socket,status);
                    }
                }
                if (state == SocketState.CLOSED) {
                    // Close socket
                    if (log.isTraceEnabled()) {
                        log.trace("Closing socket:"+socket);
                    }
                    countDownConnection();
                    try {
                        socket.getSocket().close();
                    } catch (IOException e) {
                        // Ignore
                    }
                } else if (state == SocketState.OPEN ||
                        state == SocketState.UPGRADING  ||
                        state == SocketState.UPGRADED){
                    socket.setKeptAlive(true);
                    socket.access();
                    launch = true;
                } else if (state == SocketState.LONG) {
                    socket.access();
                    waitingRequests.add(socket);
                }
            } finally {
               //other code
            }
        }
        socket = null;
        // Finish up this request
    }

}

默認情況下,代碼會運行到標註1的地方,標註1的地方又通過 org.apache.tomcat.util.net.JIoEndpoint.Handler#process 的方法進行處理,而通過前面Tomcat啓動的文章,我們已經知道handler屬性是在 org.apache.coyote.http11.Http11Protocol 的構造方法中初始化的,構造方法如下:

org.apache.coyote.http11.Http11Protocol#Http11Protocol

public Http11Protocol() {
    endpoint = new JIoEndpoint();
    cHandler = new Http11ConnectionHandler(this);
    ((JIoEndpoint) endpoint).setHandler(cHandler);
    setSoLinger(Constants.DEFAULT_CONNECTION_LINGER);
    setSoTimeout(Constants.DEFAULT_CONNECTION_TIMEOUT);
    setTcpNoDelay(Constants.DEFAULT_TCP_NO_DELAY);
}

從構造方法中,我們可以清楚的看到,其實初始化了 org.apache.coyote.http11.Http11Protocol.Http11ConnectionHandler 的實例,那麼接下來我們就來看看它的process方法,因爲Http11ConnectionHandler繼承了 org.apache.coyote.AbstractProtocol.AbstractConnectionHandler ,而自己沒有實現process方法,因此會調用到父類的process方法,那麼接下來我們就來看看AbstractConnectionHandler的process方法,代碼如下:

org.apache.coyote.AbstractProtocol.AbstractConnectionHandler#process

public SocketState process(SocketWrapper<S> socket,
        SocketStatus status) {
    Processor<S> processor = connections.remove(socket.getSocket());

    if (status == SocketStatus.DISCONNECT && processor == null) {
        //nothing more to be done endpoint requested a close
        //and there are no object associated with this connection
        return SocketState.CLOSED;
    }

    socket.setAsync(false);

    try {
        if (processor == null) {
            processor = recycledProcessors.poll();
        }
        if (processor == null) {
            processor = createProcessor();
        }

        initSsl(socket, processor);

        SocketState state = SocketState.CLOSED;
        do {
            if (status == SocketStatus.DISCONNECT &&
                    !processor.isComet()) {
                // Do nothing here, just wait for it to get recycled
                // Don't do this for Comet we need to generate an end
                // event (see BZ 54022)
            } else if (processor.isAsync() ||
                    state == SocketState.ASYNC_END) {
                state = processor.asyncDispatch(status);
            } else if (processor.isComet()) {
                state = processor.event(status);
            } else if (processor.isUpgrade()) {
                state = processor.upgradeDispatch();
            } else {
                state = processor.process(socket);
            }

            if (state != SocketState.CLOSED && processor.isAsync()) {
                state = processor.asyncPostProcess();
            }

            if (state == SocketState.UPGRADING) {
                // Get the UpgradeInbound handler
                UpgradeInbound inbound = processor.getUpgradeInbound();
                // Release the Http11 processor to be re-used
                release(socket, processor, false, false);
                // Create the light-weight upgrade processor
                processor = createUpgradeProcessor(socket, inbound);
                inbound.onUpgradeComplete();
            }
        } while (state == SocketState.ASYNC_END ||
                state == SocketState.UPGRADING);

        return state;
    } catch(java.net.SocketException e) {
              // exception handler   
    }

    return SocketState.CLOSED;
}

通過查看上面的代碼,默認一個新連接的情況下,會調用 org.apache.coyote.Processor#process 方法,而Processor的實例實在 org.apache.coyote.AbstractProtocol.AbstractConnectionHandler#createProcessor 中創建的,通過查看createProcessor代碼,我們發現是創建了一個org.apache.coyote.http11.Http11Processor的實例,那麼接下來,我們就來看看它的process方法,因爲Http11Processor繼承了AbstractHttp11Processor,最終其實調用的是AbstractHttp11Processor的process方法,代碼如下:

org.apache.coyote.http11.AbstractHttp11Processor#process

public SocketState process(SocketWrapper<S> socketWrapper)
    throws IOException {
    RequestInfo rp = request.getRequestProcessor();
    rp.setStage(org.apache.coyote.Constants.STAGE_PARSE);

    // Setting up the I/O
    // 1 
    setSocketWrapper(socketWrapper);
    getInputBuffer().init(socketWrapper, endpoint);
    getOutputBuffer().init(socketWrapper, endpoint);

    // Flags
    error = false;
    keepAlive = true;
    comet = false;
    openSocket = false;
    sendfileInProgress = false;
    readComplete = true;
    if (endpoint.getUsePolling()) {
        keptAlive = false;
    } else {
        keptAlive = socketWrapper.isKeptAlive();
    }

    if (disableKeepAlive()) {
        socketWrapper.setKeepAliveLeft(0);
    }

    while (!error && keepAlive && !comet && !isAsync() &&
            upgradeInbound == null && !endpoint.isPaused()) {

        // Parsing the request header
        try {
            setRequestLineReadTimeout();
            //2 
            if (!getInputBuffer().parseRequestLine(keptAlive)) {
                if (handleIncompleteRequestLineRead()) {
                    break;
                }
            }

            if (endpoint.isPaused()) {
                // 503 - Service unavailable
                response.setStatus(503);
                error = true;
            } else {
                // Make sure that connectors that are non-blocking during
                // header processing (NIO) only set the start time the first
                // time a request is processed.
                if (request.getStartTime() < 0) {
                    request.setStartTime(System.currentTimeMillis());
                }
                keptAlive = true;
                // Set this every time in case limit has been changed via JMX
                request.getMimeHeaders().setLimit(endpoint.getMaxHeaderCount());
                // Currently only NIO will ever return false here
                // 3
                if (!getInputBuffer().parseHeaders()) {
                    // We've read part of the request, don't recycle it
                    // instead associate it with the socket
                    openSocket = true;
                    readComplete = false;
                    break;
                }
                if (!disableUploadTimeout) {
                    setSocketTimeout(connectionUploadTimeout);
                }
            }
        } catch (IOException e) {
            if (getLog().isDebugEnabled()) {
                getLog().debug(
                        sm.getString("http11processor.header.parse"), e);
            }
            error = true;
            break;
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            UserDataHelper.Mode logMode = userDataHelper.getNextMode();
            if (logMode != null) {
                String message = sm.getString(
                        "http11processor.header.parse");
                switch (logMode) {
                    case INFO_THEN_DEBUG:
                        message += sm.getString(
                                "http11processor.fallToDebug");
                        //$FALL-THROUGH$
                    case INFO:
                        getLog().info(message);
                        break;
                    case DEBUG:
                        getLog().debug(message);
                }
            }
            // 400 - Bad Request
            response.setStatus(400);
            adapter.log(request, response, 0);
            error = true;
        }

        if (!error) {
            // Setting up filters, and parse some request headers
            rp.setStage(org.apache.coyote.Constants.STAGE_PREPARE);
            try {
                prepareRequest();
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                if (getLog().isDebugEnabled()) {
                    getLog().debug(sm.getString(
                            "http11processor.request.prepare"), t);
                }
                // 400 - Internal Server Error
                response.setStatus(400);
                adapter.log(request, response, 0);
                error = true;
            }
        }

        if (maxKeepAliveRequests == 1) {
            keepAlive = false;
        } else if (maxKeepAliveRequests > 0 &&
                socketWrapper.decrementKeepAlive() <= 0) {
            keepAlive = false;
        }

        // Process the request in the adapter
        if (!error) {
            try {
                // 4
                rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
                adapter.service(request, response);
                // Handle when the response was committed before a serious
                // error occurred.  Throwing a ServletException should both
                // set the status to 500 and set the errorException.
                // If we fail here, then the response is likely already
                // committed, so we can't try and set headers.
                if(keepAlive && !error) { // Avoid checking twice.
                    error = response.getErrorException() != null ||
                            (!isAsync() &&
                            statusDropsConnection(response.getStatus()));
                }
                setCometTimeouts(socketWrapper);
            } catch (InterruptedIOException e) {
                error = true;
            } catch (HeadersTooLargeException e) {
                error = true;
                // The response should not have been committed but check it
                // anyway to be safe
                if (!response.isCommitted()) {
                    response.reset();
                    response.setStatus(500);
                    response.setHeader("Connection", "close");
                }
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                getLog().error(sm.getString(
                        "http11processor.request.process"), t);
                // 500 - Internal Server Error
                response.setStatus(500);
                adapter.log(request, response, 0);
                error = true;
            }
        }

        // Finish the handling of the request
        rp.setStage(org.apache.coyote.Constants.STAGE_ENDINPUT);

        if (!isAsync() && !comet) {
            if (error) {
                // If we know we are closing the connection, don't drain
                // input. This way uploading a 100GB file doesn't tie up the
                // thread if the servlet has rejected it.
                getInputBuffer().setSwallowInput(false);
            }
            endRequest();
        }

        rp.setStage(org.apache.coyote.Constants.STAGE_ENDOUTPUT);

        // If there was an error, make sure the request is counted as
        // and error, and update the statistics counter
        if (error) {
            response.setStatus(500);
        }
        request.updateCounters();

        if (!isAsync() && !comet || error) {
            getInputBuffer().nextRequest();
            getOutputBuffer().nextRequest();
        }

        if (!disableUploadTimeout) {
            if(endpoint.getSoTimeout() > 0) {
                setSocketTimeout(endpoint.getSoTimeout());
            } else {
                setSocketTimeout(0);
            }
        }

        rp.setStage(org.apache.coyote.Constants.STAGE_KEEPALIVE);

        if (breakKeepAliveLoop(socketWrapper)) {
            break;
        }
    }

    rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);

    if (error || endpoint.isPaused()) {
        return SocketState.CLOSED;
    } else if (isAsync() || comet) {
        return SocketState.LONG;
    } else if (isUpgrade()) {
        return SocketState.UPGRADING;
    } else {
        if (sendfileInProgress) {
            return SocketState.SENDFILE;
        } else {
            if (openSocket) {
                if (readComplete) {
                    return SocketState.OPEN;
                } else {
                    return SocketState.LONG;
                }
            } else {
                return SocketState.CLOSED;
            }
        }
    }
}

上面的代碼有點長,但是經過分析,我們還是可以看清楚主幹,我已經在代碼中將主流程通過數字標註了,我們就來一一看看標註了數字的地方:

標註1的地方(第7行)將Socket的輸入流和輸出流通過InternalInputBuffer進行了包裝,InternalInputBuffer是在Http11Processor的構造函數中初始化的。
標註2的地方(第35行)調用了InternalInputBuffer的parseRequesLine方法解析http請求的請求行。(關於http請求行和請求頭請看下文解釋)
標註3的地方(第57行)調用了InternalInputBuffer的prarseHeaders方法解析http請求的請求頭。解析完了以後,會將http header保存在 org.apache.tomcat.util.http.MimeHeaders
標註4的地方(第128行)調用了org.apache.coyote.Adapter#service方法,次方法就會最終調用到具體的Servlet.

對於Http請求行和請求頭,大家可以看下面的例子:
Http get request

GET /contextpath/querystring HTTP/1.1

Host: 127.0.0.1:8080

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:23.0) Gecko/20100101 Firefox/23.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-US,en;q=0.5

Accept-Encoding: gzip, deflate

Cookie: JSESSIONID=9F5897FEF3CDBCB234C050C132DCAE52; __atuvc=384%7C39; __utma=96992031.358732763.1380383869.1381468490.1381554710.38; __utmz=96992031.1380383869.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_21e144d0df165d6556d664e2836dadfe=1381330561,1381368826,1381395666,1381554711

Connection: keep-alive

Cache-Control: max-age=0

在上面的Http協議get請求中,其中請求行就是第一行, GET /contextpath/querystring HTTP/1.1 ,餘下的都是請求頭。這裏面需要注意根據Http協議的要求,請求行末尾必須是CRLF,而請求行與請求頭,以及請求頭之間必須用空行隔開,而空行也必須只包含CRLF。對於Http協議請求頭的規範可以參考 這裏 。

通過上面的描述,我們可以整理出如下的一個請求解析流程:
Request http header parse

org.apache.tomcat.util.net.JIoEndpoint.Acceptor#run
->org.apache.tomcat.util.net.JIoEndpoint.SocketProcessor#run(請求處理線程池中運行)
-->org.apache.coyote.AbstractProtocol.AbstractConnectionHandler#process
--->org.apache.coyote.http11.AbstractHttp11Processor#process
---->org.apache.coyote.http11.InternalInputBuffer#parseRequestLine
---->org.apache.coyote.http11.InternalInputBuffer#parseHeaders
---->org.apache.catalina.connector.CoyoteAdapter#service

如何轉發到Servlet

上面我們說了一個請求過來是如何根據http協議解析Socket的數據,最終將生成 org.apache.coyote.Request 和 org.apache.coyote.Response ,接下來我們就來看看request,reponse是如何一步步的進入最終的Servlet進行處理的。這一步的入口就是CoyoteAdapter的service方法。 接下來我們就來看看它的代碼:

org.apache.catalina.connector.CoyoteAdapter#service

public void service(org.apache.coyote.Request req,
                    org.apache.coyote.Response res)
    throws Exception {


    Request request = (Request) req.getNote(ADAPTER_NOTES);
    Response response = (Response) res.getNote(ADAPTER_NOTES);

    //1 
    if (request == null) {

        // Create objects
        request = connector.createRequest();
        request.setCoyoteRequest(req);
        response = connector.createResponse();
        response.setCoyoteResponse(res);

        // Link objects
        request.setResponse(response);
        response.setRequest(request);

        // Set as notes
        req.setNote(ADAPTER_NOTES, request);
        res.setNote(ADAPTER_NOTES, response);

        // Set query string encoding
        req.getParameters().setQueryStringEncoding
            (connector.getURIEncoding());

    }

    if (connector.getXpoweredBy()) {
        response.addHeader("X-Powered-By", POWERED_BY);
    }

    boolean comet = false;
    boolean async = false;

    try {

        // Parse and set Catalina and configuration specific
        // request parameters
        req.getRequestProcessor().setWorkerThreadName(Thread.currentThread().getName());
        //2
        boolean postParseSuccess = postParseRequest(req, request, res, response);
        if (postParseSuccess) {
            //check valves if we support async
            request.setAsyncSupported(connector.getService().getContainer().getPipeline().isAsyncSupported());
            // Calling the container
            //3
            connector.getService().getContainer().getPipeline().getFirst().invoke(request, response);

            // other code

        }
        // other code

    } catch (IOException e) {
        // Ignore
    } finally {
        req.getRequestProcessor().setWorkerThreadName(null);
        // Recycle the wrapper request and response
        if (!comet && !async) {
            request.recycle();
            response.recycle();
        } else {
            // Clear converters so that the minimum amount of memory
            // is used by this processor
            request.clearEncoders();
            response.clearEncoders();
        }
    }

}

爲了可以清楚的看到主流程,上面刪除了一部分非主流程的代碼,接下來我們逐一分析一下標註了數字的地方:

標註1的代碼(第9行)將 org.apache.coyote.Request 和 org.apache.coyote.Response 對象轉變爲 org.apache.catalina.connector.Request , org.apache.catalina.connector.Response 類型的對象。其中coyote包中的Request僅僅只是包含了解析出來的http協議的數據,而connector包中的Request纔是真正Servlet容器中的HttpServletRequest,它裏面包含了完成請求需要的host,context和wrapper信息,在這裏每一個wrapper其實都對應web.xml配置的一個Servlet。
標註2(第44行)的代碼調用了postParseRequest方法,這個方法裏面做的事情非常多,但是最終都是爲了根據Request對象找到對應的Host,Conext和Wrapper對象,也就是說最終要清楚這個請求應該由哪個Servlet來處理。
標註3(第50)的代碼將已經設置好了Host,Context,Wrapper對象的Request通過Pipeline機制鏈式傳遞給最終的Servlet。

上面只是從整體上告訴了讀者 org.apache.catalina.connector.CoyoteAdapter#service 方法做的事情,接下來我們進一步分解每一個步驟都具體做了哪些工作。第一步比較簡單,大家可以自己閱讀,我們關鍵來看2,3步。首先我們來看看postParseRequest方法。 通過分析org.apache.catalina.connector.CoyoteAdapter#postParseRequest的代碼,我們會發現它最終是通過 org.apache.tomcat.util.http.mapper.Mapper#map 方法來達到匹配請求到對應的Context和Wrapper(Servlet包裝類)目的。具體代碼如下:

org.apache.catalina.connector.CoyoteAdapter#postParseRequest

connector.getMapper().map(serverName, decodedURI, version,
                                      request.getMappingData());
            request.setContext((Context) request.getMappingData().context);
            request.setWrapper((Wrapper) request.getMappingData().wrapper);

那我們再來看看此方法。通過分析它的代碼,我們發現最終其實是調用了幾個 internalMap** 方法將找到的Context,Wrapper設置到org.apache.catalina.connector.Request對象的org.apache.tomcat.util.http.mapper.MappingData類型的屬性中,map方法執行完以後,然後接下來就從MappingData中獲取已經找到的Context和Wrapper,再設置到Request的context和wrapper中。

接下來我們再來分析第3步,第3步通過pipeline鏈式調用機制最終調用了Servlet對象,而對於pipeline其實是運用了責任鏈模式,它將各個閥門鏈接起來,然後一步步的調用,而至於有多少個閥門(Valve)對象,主要來源於兩個地方,一個是conf/server.xml中配置的valve,我們知道所有的容器都是支持pipeline機制的,另外一個就是每一個容器的構造其中自己初始化的閥門對象。接下來一一看一下。對於StandardEngine來說有一個與之對應的StandardEngineValve,對於StandardHost有一個StandardHostValve與之對應,StandardContext有一個StandardContextValve與之對應,StandardWrapper與StandardWrapperValve對應,通過分析代碼,我們可以得到如下的一個調用鏈。

->org.apache.catalina.core.StandardEngineValve#invoke
-->org.apache.catalina.valves.AccessLogValve#invoke
--->org.apache.catalina.valves.ErrorReportValve#invoke
---->org.apache.catalina.core.StandardHostValve#invoke
----->org.apache.catalina.authenticator.AuthenticatorBase#invoke
------>org.apache.catalina.core.StandardContextValve#invoke
------->org.apache.catalina.core.StandardWrapperValve#invoke

上述的調用棧中,最後會調用到StandardWrapperValve,它其實也是最終調用Servlet的地方,接下來我們就來看看它的代碼:

public final void invoke(Request request, Response response)
    throws IOException, ServletException {

    // Initialize local variables we may need
    boolean unavailable = false;
    Throwable throwable = null;
    // This should be a Request attribute...
    long t1=System.currentTimeMillis();
    requestCount++;
    StandardWrapper wrapper = (StandardWrapper) getContainer();
    Servlet servlet = null;
    Context context = (Context) wrapper.getParent();


    // Allocate a servlet instance to process this request
    try {
        //1
        if (!unavailable) {
            servlet = wrapper.allocate();
        }
    } catch (UnavailableException e) {
        container.getLogger().error(
                sm.getString("standardWrapper.allocateException",
                        wrapper.getName()), e);
        long available = wrapper.getAvailable();
        if ((available > 0L) && (available < Long.MAX_VALUE)) {
            response.setDateHeader("Retry-After", available);
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,
                       sm.getString("standardWrapper.isUnavailable",
                                    wrapper.getName()));
        } else if (available == Long.MAX_VALUE) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND,
                       sm.getString("standardWrapper.notFound",
                                    wrapper.getName()));
        }
    } // other code

    MessageBytes requestPathMB = request.getRequestPathMB();
    DispatcherType dispatcherType = DispatcherType.REQUEST;
    if (request.getDispatcherType()==DispatcherType.ASYNC) dispatcherType = DispatcherType.ASYNC;
    request.setAttribute(Globals.DISPATCHER_TYPE_ATTR,dispatcherType);
    request.setAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR,
            requestPathMB);
    // Create the filter chain for this request
    ApplicationFilterFactory factory =
        ApplicationFilterFactory.getInstance();
    ApplicationFilterChain filterChain =
        factory.createFilterChain(request, wrapper, servlet);

    // Reset comet flag value after creating the filter chain
    request.setComet(false);

    // Call the filter chain for this request
    // NOTE: This also calls the servlet's service() method
    // 2 
    try {
        if ((servlet != null) && (filterChain != null)) {
            // Swallow output if needed
            if (context.getSwallowOutput()) {
                try {
                    SystemLogHandler.startCapture();
                    if (request.isAsyncDispatching()) {
                        //TODO SERVLET3 - async
                        ((AsyncContextImpl)request.getAsyncContext()).doInternalDispatch();
                    } else if (comet) {
                        filterChain.doFilterEvent(request.getEvent());
                        request.setComet(true);
                    } else {
                        filterChain.doFilter(request.getRequest(),
                                response.getResponse());
                    }
                } finally {
                    String log = SystemLogHandler.stopCapture();
                    if (log != null && log.length() > 0) {
                        context.getLogger().info(log);
                    }
                }
            } else {
                if (request.isAsyncDispatching()) {
                    //TODO SERVLET3 - async
                    ((AsyncContextImpl)request.getAsyncContext()).doInternalDispatch();
                } else if (comet) {
                    request.setComet(true);
                    filterChain.doFilterEvent(request.getEvent());
                } else {
                    filterChain.doFilter
                        (request.getRequest(), response.getResponse());
                }
            }

        }
    } catch(Exception e){
    // other code
    }


}

爲了節省版面,上面的代碼已經刪除非主流程的代碼。接下來我們逐一分析一下標註了數字的地方:

標註1(第17行)的代碼實例化了Servlet對象,在實例化的過程中使用了Java雙檢查鎖的機制來實例化Servlet,有興趣的童鞋可以去看看org.apache.catalina.core.StandardWrapper#allocate的代碼。這裏需要注意的是在Servlet2.4規範之前,有一個singleThreadMode模型,這個機制類似與之前EJB的無狀態會話Bean機制,每個線程過來會通過實例池中取出一個實例來完成響應。在Servlet規範2.4之後,單線程模型已經被廢除了。具體細節可以參考 這裏 .
標註2(第55行)的代碼其實調用了大家熟悉的Servlet的過濾器鏈,過濾器鏈最終就會調用到Servlet.

最後,咋們再來看看過濾器濾鏈的處理,來看看 org.apache.catalina.core.ApplicationFilterChain#doFilter ,doFilter方法中會根據filterConfig中取的web.xml配置的過濾器,然後一個個調用,等每個過濾器執行完了以後,最終就會調用到Servlet的Service方法。

通過上面的分析,其實我們已經清楚了一個請求過來以後,Tomcat是如何一步步處理的。我們再來做一個總體的總結:

用戶瀏覽器發送請求,請求會發送到對應的Connector監聽的Socket端口。
Connector從Socket流中獲取數據,然後根據Http協議將其解析爲Request和Reponse對象
找到Request對象對應的Host,Context,Wrapper
調用最終的Servelt的service進行處理。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章