1. 程式人生 > >Tomcat執行緒池配置(Tomcat6)

Tomcat執行緒池配置(Tomcat6)

1、執行緒池配置(Tomcat6下)

使用執行緒池,用較少的執行緒處理較多的訪問,可以提高tomcat處理請求的能力。使用方式:

首先。開啟/conf/server.xml,增加

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="500" minSpareThreads="20" maxIdleTime="60000" />

最大執行緒500(一般伺服器足以),最小空閒執行緒數20,執行緒最大空閒時間60秒。

然後,修改<Connector ...>節點,增加executor屬性,如:

<Connector executor="tomcatThreadPool"
               port="80"

protocol="HTTP/1.1"

maxThreads="600"

minSpareThreads="100"

maxSpareThreads="300"
               connectionTimeout="60000"
               keepAliveTimeout="15000"
               maxKeepAliveRequests="1"
               redirectPort="443"
               ....../>

maxThreads:Tomcat可建立的最大的執行緒數,每一個執行緒處理一個請求;

minSpareThreads:最小備用執行緒數,tomcat啟動時的初始化的執行緒數;

maxSpareThreads:最大備用執行緒數,一旦建立的執行緒超過這個值,Tomcat就會關閉不再需要的socket執行緒;

acceptCount:指定當所有可以使用的處理請求的執行緒數都被使用時,可以放到處理佇列中的請求數,就是被排隊的請求數,超過這個數的請求將拒絕連線。

connnectionTimeout:網路連線超時,單位:毫秒。設定為0表示永不超時,這樣設定有隱患的。通常可設定為30000毫秒。
enableLookups:是否允許DNS查詢

注意:可以多個connector公用1個執行緒池。

2、調整連線相關Connector的引數:

<Connector executor="tomcatThreadPool"
               port="80" protocol="HTTP/1.1"
               connectionTimeout="60000"
               keepAliveTimeout="15000"
               maxKeepAliveRequests="1"
               redirectPort="443"
               maxHttpHeaderSize="8192" URIEncoding="UTF-8" enableLookups="false" acceptCount="100" disableUploadTimeout="true"/>

引數說明:

  • connectionTimeout - 網路連線超時,單位:毫秒。設定為0表示永不超時,這樣設定有隱患的。通常可設定為30000毫秒。
  • keepAliveTimeout - 長連線最大保持時間(毫秒)。此處為15秒。
  • maxKeepAliveRequests - 最大長連線個數(1表示禁用,-1表示不限制個數,預設100個。一般設定在100~200之間) the maximum number of HTTP requests that can be held in the pipeline until the connection is closed by the server. Setting this attribute to 1 disables HTTP/1.0 keep-alive, as well as HTTP/1.1 keep-alive and pipelining. Setting this to -1 allows an unlimited number of pipelined or keep-alive HTTP requests. If not specified, this attribute is set to 100.
  • maxHttpHeaderSize - http請求頭資訊的最大程度,超過此長度的部分不予處理。一般8K。
  • URIEncoding - 指定Tomcat容器的URL編碼格式。
  • acceptCount - 指定當所有可以使用的處理請求的執行緒數都被使用時,可以放到處理佇列中的請求數,超過這個數的請求將不予處理,預設為10個。defines the maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full are refused. The default value is 10.
  • disableUploadTimeout - 上傳時是否使用超時機制
  • enableLookups - 是否反查域名,取值為:true或false。為了提高處理能力,應設定為false
  • bufferSize - defines the size (in bytes) of the buffer to be provided for input streams created by this connector. By default, buffers of 2048 bytes are provided.
  • maxSpareThreads - 做多空閒連線數,一旦建立的執行緒超過這個值,Tomcat就會關閉不再需要的socket執行緒 the maximum number of unused request processing threads that are allowed to exist until the thread pool starts stopping the unnecessary threads. The default value is 50.
  • maxThreads - 最多同時處理的連線數,Tomcat使用執行緒來處理接收的每個請求。這個值表示Tomcat可建立的最大的執行緒數。。 the maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200.
  • minSpareThreads - 最小空閒執行緒數,Tomcat初始化時建立的執行緒數 the number of request processing threads that are created when this Connector is first started. The connector will also make sure it has the specified number of idle processing threads available. This attribute should be set to a value smaller than that set for maxThreads. The default value is 4.
  • minProcessors - 最小空閒連線執行緒數,用於提高系統處理效能,預設值為10。(用於Tomcat4中)
  • maxProcessors - 最大連線執行緒數,即:併發處理的最大請求數,預設值為75。(用於Tomcat4中)

備註:

Tomcat4中可以通過修改minProcessors和maxProcessors的值來控制執行緒數。

在Tomcat5+主要對以下引數調整
maxThreads
 Tomcat使用執行緒來處理接收的每個請求。這個值表示Tomcat可建立的最大的執行緒數。
 acceptCount
 指定當所有可以使用的處理請求的執行緒數都被使用時,可以放到處理佇列中的請求數,超過這個數的請求將不予處理。
 connnectionTimeout
 網路連線超時,單位:毫秒。設定為0表示永不超時,這樣設定有隱患的。通常可設定為30000毫秒。
 minSpareThreads
 Tomcat初始化時建立的執行緒數。
 maxSpareThreads
 一旦建立的執行緒超過這個值,Tomcat就會關閉不再需要的socket執行緒。  

轉自:http://blog.csdn.net/chinadeng/article/details/6591542