5

Tomcat 和 appache2 通过 AJP 连接。我有这些用于 tomcat 和 apache 的配置:

<Connector port="8009" protocol="AJP/1.3"
           URIEncoding="UTF-8"
           redirectPort="8443"
           connectionTimeout="20000"
           maxThreads="512" />


<IfModule mpm_event_module>
    StartServers          5
    MinSpareThreads      25
    MaxSpareThreads      75
    ThreadLimit          64
    ThreadsPerChild      25
    ServerLimit        1024
    MaxClients          512
    MaxRequestsPerChild   0
</IfModule>

当我去 tomcat manager 时,我看到:

"ajp-bio-8009"
Max threads: 512 Current thread count: 256 Current thread busy: 231

当前线程busy有时会达到256。为什么当前线程数没有设置为512?

4

1 回答 1

2

当前线程由apache动态管理。

来自apache源代码中的worker.c:线程根据需要启动。以下是启动工作线程的主要流程。

static int worker_run(apr_pool_t *_pconf, apr_pool_t *plog, server_rec *s)
  startup_children(remaining_children_to_start);  //server/mpm/worker/worker.c
     if (make_child(ap_server_conf, i) < 0) {     //server/mpm/worker/worker.c
       child_main(slot);                          //server/mpm/worker/worker.c
         rv = apr_thread_create(&start_thread_id, thread_attr, start_threads
         rv = apr_thread_create(&threads[i], thread_attr, worker_thread, my_info, pchild);     

并且当空闲线程> max_spare_threads 时,apache 将尝试通过杀死子进程/线程来减少空闲线程以减少空闲线程数。

if (idle_thread_count > max_spare_threads) {
    /* Kill off one child */
    ap_worker_pod_signal(pod, TRUE);
    retained->idle_spawn_rate = 1;
}

apache在内部管理这些子进程,并使用信号与子进程通信以根据需要调整线程。所有线程都在ap_scoreboard_image->servers[i][j];

apache 将监视线程数,并使用套接字发送让 infoCollector 知道。大致通过以下代码(删除了无关的行)

./httpd/modules/cluster/mod_heartbeat.c
    for (i = 0; i < ctx->server_limit; i++) {
         ....
            for (j = 0; j < ctx->thread_limit; j++) {
                ws = &ap_scoreboard_image->servers[i][j];
                if (res == SERVER_READY && ps->generation == mpm_generation) {
                    ready++;
                }
            }
        }

    len = apr_snprintf(buf, sizeof(buf), "v=%u&ready=%u&busy=%u", MSG_VERSION, ready, busy);
    ...
    rv = apr_socket_sendto(sock, ctx->mcast_addr, 0, buf, &len);

在 Tomcat 中: 这些线程信息通过以下方式接收:./tomcat/java/org/apache/catalina/ha/backend/CollectedInfo.java 并通过以下方式显示:/Users/twer/lab/tomcat/java/org/apache/catalina/经理/StatusTransformer.java

writer.write(" currentThreadCount=\"" + mBeanServer.getAttribute(tpName, "currentThreadCount") + "\"");

这里显示的大部分代码都是主要流程,无关的行被删除。下载源代码以了解更多信息。下载apache src:http ://www.apache.org/dist/httpd/?C=S;O=A 下载tomcat src:http: //tomcat.apache.org/download-70.cgi

于 2012-12-06T13:47:10.083 回答