For Tomcat, each incoming request requires a thread until the request ends.

Source: Internet
Author: User

This is a time of stress testing for Java Web applications, and the deployment container is Tomcat 7. During the period of learning a lot of scattered knowledge points, timely carding summary, build a good and easy to understand the knowledge structure to organize them, for forgetting.
For Web application developers, we are concerned about the number of requests that the app can process simultaneously, and the response time. The application itself and the Web container in which it runs are two very important factors.
For Tomcat, each incoming request requires a thread until the request ends. If more requests come in at the same time than the currently available request processing threads, the additional threads are created until the maximum number of threads configured (the MaxThreads property value) is reached. If more requests are still received at the same time, these late-processing requests accumulate in the ServerSocket created by connector until the maximum configuration value (Acceptcount attribute value) is reached. At this point, any requests that come back will receive connection refused errors until there are resources available to process them.

Analyze, comb, organize

What we are concerned about is the number of requests that Tomcat can handle simultaneously and the response time of the request, apparently the MaxThreads and Acceptcount attributes of the connector element have a direct effect on it. Regardless of the Acceptcount value, MaxThreads directly determines the number of requests that can actually be processed concurrently. Regardless of the MaxThreads, Acceptcount determines how many requests can wait to be processed. However, whether a request can be processed immediately or in a waiting area, Tomcat will need to accept the request first (that is, accept the client's connection request, establish Socketchannel), then the number of connections that Tomcat can make (MaxConnections attribute value) Also affects the number of requests that can be processed concurrently.
We can think of Tomcat as a hospital, you come to the hospital hall registered doctor, if they accept, it is equivalent to the client and server to establish a socket connection. Then you come to the appropriate department, each doctor in the department has a consulting room, which is equivalent to processing the request of the thread; If all the clinics have patients, the department's dispatching nurses will let you wait in the Department hall, until they inform you to the number of consultation room, if you have a free doctor, you can immediately visit.
Some patients to the hospital is very hasty, the results of his turn to register or a doctor, he still in the bag to look for cases and health insurance card, if more than the nurses or doctors can withstand the waiting time, they will let patients go to the side to find, first service inferior. This situation is similar to the ConnectionTimeout attribute of the connector element. A time-out exception is thrown if the current connector (Connector) waits for a specified time after the connection is accepted, but still does not receive RequestUri line.

Knowledge Point Collection

The configuration reference documentation for Tomcat 7 has been described in detail for the relevant properties, which are gathered here:

Protocol

Sets the protocol to handle incoming traffic. The default Valueis http/1.1 which uses an auto-switching mechanism to select either a Blockingjava based connector or an Apr/native based connector. If the PATH (Windows) Orld_library_path (on most UNIX systems) environment variables contain the tomcatnative LIBRARY, the Apr/native connector'll be used. If the native librarycannot be found, the blocking Java based connector would be used. Note that theapr/native connector have different settings for HTTPS than the Java connectors.
To use a explicit protocol rather than rely on the auto-switching mechanismdescribed above, the following values could be U Sed:
org.apache.coyote.http11.http11protocol-blocking Java connector
Org.apache.coyote.http11.http11nioprotocol-non Blocking Java Connector
Org.apache.coyote.http11.http11aprprotocol-the apr/native connector.

Custom implementations may also is used.
Take a look at our Connector Comparison chart. The configuration for both javaconnectors are identical, for HTTP and HTTPS.
For more information on the APR connector and APR specific SSL settings pleasevisit the APR documentation

MaxThreads

The maximum number of request processing threads to being createdby this Connector, which therefore determines the maximum Nu Mber ofsimultaneous requests that can be handled. If not specified, this attribute isset to 200. If an executor are associated with this connector, this attribute isignored as the connector would execute tasks using the E Xecutor rather than aninternal thread pool.

Acceptcount

The maximum queue length for incoming connection requests whenall possible request processing threads is in use. Any requests received whenthe queue are full would be refused. The default value is 100.

MaxConnections

The maximum number of connections that the server would acceptand process at any given time.When this number has beenreached, the server would accept, but not process, one further connection. Thisadditional connection is blocked until the number of connections beingprocessed falls below maxconnections at which PO int the server would startaccepting and processing new connections again. Note that once the limit Hasbeen reached, the operating system could still accept connections based on Theacceptcount settin G.Thedefault value varies by connector type.For BIO The default was the value of MaxThreads unless an Executor isused in which case the default would be the value O F MaxThreads from Theexecutor. For NIO, the default is 10000. For apr/native, the default is 8192.
Note that for apr/native on Windows, the configured value would be reduced tothe highest multiple of 1024x768 that's less than or equal to MaxConnections. Thisis done for performance reasons.
If set to a value of of-1, the MaxConnections feature is disabled and connectionsare not counted.

ConnectionTimeout

The number of milliseconds this Connector would wait, after accepting a connection, for the request UR I LineTo be presented. Use a value of of-1 to indicate no (i.e.infinite) timeout. The default value is 60000 (i.e. seconds), but note thatthe standard Server.xml, ships with Tomcat sets this to 2000 0 (i.e. 20seconds). Unless disableuploadtimeout is set to False, this timeout willalso being used when reading the request body (if any) .

Further analysis

Tomcat's HTTP Connector has three types: BIO, NiO, Apr. As you can see from the attribute descriptions above, the same properties may have different default values and different processing strategies for different connector implementations, so before you adjust the configuration, you need to figure out the difference between the various implementations and what kind of connector the current deployment container is using.
Consult the TOMCAT7 HTTP Connector configuration Document Connector comparison section to learn about the differences between the various connector implementations.
How do you know what connector implementation the container is using? After you start Tomcat, access the server Status Page and see the following information to know what connector is being used:

My OS is windows, so Tomcat uses Aprconnector by default. On Linux, the Bio Connector is used by default. Compared with NIO, Bio has a low performance. Insert the following configuration fragment in <tomcat_home>/conf/server.xml:

<connectorport= "8080" protocol= "http/1.1"
connectiontimeout= "20000"
redirectport= "8443"/>

Modified to:

<connectorport= "8080" protocol= "Org.apache.coyote.http11.Http11NioProtocol"
connectiontimeout= "20000"
redirectport= "8443"/>

You can switch the HTTP connector to NIO. For more details, refer to modifying Tomcat connector run mode to optimize Tomcat run performance

Website Description: http://tomcat.apache.org/tomcat-7.0-doc/config/http.html

One, the maximum number of threads set

The Server.xml connectors in Tomcat are set as follows

<connectorport= "8080" maxthreads= "minsparethreads=" "maxsparethreads=" "enablelookups=" "false" Redirectport= "8443" acceptcount= "debug=" 0 "connectiontimeout=" 20000 "disableuploadtimeout=" true "/>

When Tomcat sets the maximum number of threads during configuration and the current number of threads exceeds this value, is there any way to catch this error and display the error message on the client side?

2. How to increase the number of Tomcat connections
In the <connector/> configuration in Tomcat configuration file Server.xml, the parameters related to the number of connections are:
Minprocessors: Minimum number of idle connection threads for improved system processing performance with a default value of 10
Maxprocessors: Maximum number of connection threads, which is the maximum number of requests concurrently processed, with a default value of 75
Acceptcount: Maximum number of connections allowed, should be greater than or equal to Maxprocessors, default value is 100
Enablelookups: Whether to reverse the domain name, the value is: TRUE or false. To improve processing power, set to False
ConnectionTimeout: Network connection time-out, unit: milliseconds. Set to 0 to never time out, so the setting is hidden. It can usually be set to 30000 milliseconds.
The parameters associated with the maximum number of connections are maxprocessors and Acceptcount. If you want to increase the number of concurrent connections, both parameters should be increased.
The maximum number of connections allowed by Web server is also subject to the kernel parameter settings of the operating system, usually Windows is about 2000, and Linux is about 1000. Examples of configurations in TOMCAT5:
<connector port= "8080"
maxthreads= "minsparethreads=" maxsparethreads= "75"
Enablelookups= "false" redirectport= "8443" acceptcount= "100"
debug= "0" connectiontimeout= "20000"
Disableuploadtimeout= "true"/>
The listening configuration for the other ports, and so on.

3. How to disable files in a column directory in Tomcat
In {tomcat_home}/conf/web.xml, set the listings parameter to False, as follows:

1. <init-param>

2. <param-name>listings</param-name>

3. <param-value>false</param-value>

4. </init-param>

4. How to increase the memory that Tomcat can use
Tomcat can use 128MB of memory by default, and in larger applications, this memory is not enough and needs to be scaled up.
Under Unix, in front of the file {tomcat_home}/bin/catalina.sh, add the following settings:
java_opts= '-xms ' Initialize memory size "-XMX" maximum memory that can be used "'
You need to increase the value of this two parameter. For example:
java_opts= '-xms256m-xmx512m '
Indicates that the initialized memory is 256MB and the maximum memory that can be used is 512MB

For Tomcat, each incoming request requires a thread until the request ends.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.