tomcat 9同时处理请求还是序列?

时间:2016-12-21 13:12:30

标签: tomcat

https://github.com/apache/tomcat/blob/de6baf6e3a136b72a27ac788abc9955f4061ecb5/java/org/apache/catalina/core/StandardServer.java

从下面的源代码中看来,如果有两个请求同时出现,那么第一个请求就会调用socket = serverSocket.accept(),在完成第一个请求的处理后,它会处理第二个请求通过调用serverSocket.accept(),所以看起来tomcat在await()方法中逐个处理请求,而不是每个线程一个请求同时处理它们,但是从下面的文档中,它说明“他的ThreadPoolExecutor指定了一个TaskThread来处理请求。”但是我没有看到ThreadPoolExecutor如何分配一个TaskThread来处理请求。任何人都可以给出一些暗示tomcat如何从源代码的角度将每个请求分配给一个线程? https://github.com/apache/tomcat/blob/83b3ea892aa03b4a8bbfdfe2b9a2e28755cb52cc/webapps/docs/architecture/startup/serverStartup.txt

    try {
        awaitThread = Thread.currentThread();

        // Loop waiting for a connection and a valid command
        while (!stopAwait) {
            ServerSocket serverSocket = awaitSocket;
            if (serverSocket == null) {
                break;
            }

            // Wait for the next connection
            Socket socket = null;
            StringBuilder command = new StringBuilder();
            try {
                InputStream stream;
                long acceptStartTime = System.currentTimeMillis();
                try {
                    **socket = serverSocket.accept();**
                    socket.setSoTimeout(10 * 1000);  // Ten seconds
                    stream = socket.getInputStream();
                } catch (SocketTimeoutException ste) {
                    // This should never happen but bug 56684 suggests that
                    // it does.
                    log.warn(sm.getString("standardServer.accept.timeout",
                            Long.valueOf(System.currentTimeMillis() - acceptStartTime)), ste);
                    continue;
                } catch (AccessControlException ace) {
                    log.warn("StandardServer.accept security exception: "
                            + ace.getMessage(), ace);
                    continue;
                } catch (IOException e) {
                    if (stopAwait) {
                        // Wait was aborted with socket.close()
                        break;
                    }
                    log.error("StandardServer.await: accept: ", e);
                    break;
                }

                // Read a set of characters from the socket
                int expected = 1024; // Cut off to avoid DoS attack
                while (expected < shutdown.length()) {
                    if (random == null)
                        random = new Random();
                    expected += (random.nextInt() % 1024);
                }
                while (expected > 0) {
                    int ch = -1;
                    try {
                        ch = stream.read();
                    } catch (IOException e) {
                        log.warn("StandardServer.await: read: ", e);
                        ch = -1;
                    }
                    // Control character or EOF (-1) terminates loop
                    if (ch < 32 || ch == 127) {
                        break;
                    }
                    command.append((char) ch);
                    expected--;
                }
            } finally {
                // Close the socket now that we are done with it
                try {
                    if (socket != null) {
                        socket.close();
                    }
                } catch (IOException e) {
                    // Ignore
                }
            }

1 个答案:

答案 0 :(得分:0)

您对正在阅读的代码感到困惑。

上面代码段中的代码正在等待来自本地连接的SHUTDOWN命令。这与处理HTTP请求无关。

你是对的:SHUTDOWN监听器是单线程的,一次只接受一个连接。但是,在服务器的生命周期中,人们真的只希望收到一个SHUTDOWN命令。

查看Tomcat configuration reference for the Server component,尤其是portshutdown设置。