** Base on:
http://jakarta.apache.org/tomcat/tomcat-3.3-doc/tomcat-ug.html
Please see this for the complete Tomcat User Guide.
Real world configuration tips
By default the Tomcat distribution comes with a naive configuration whose main
goal is to promote first time user experience and an "out of the box"
operation... This configuration however is not the best way to deploy Tomcat
on real sites. For example, real sites may require some performance tuning
and site-specific settings (additional path elements for example). This section
will try to get you started by directing you to the first steps that should
be taken before publishing a Tomcat based site.
Modify and customize the batch files
As stated in the previous sections, the startup scripts are here for your
convenience. Yet, sometimes the scripts that are needed for deployment should
be modified for the following purposes:
- To set resource limits such as maximum number of descriptors.
- To add new CLASSPATH entries (for example, JDBC drivers).
- To add new PATH/LD_LIBRARY_PATH entries (for example, JDBC drivers DLLs).
- To modify the JVM command line settings.
- Make sure that you are using a specific JVM (out of the two or three JVMs
installed on your machine).
- To switch user from root to some other user using the "su" UNIX® command.
- Your pet reason.
Some of these changes can be done without explicit changes to the basic
scripts; for example, the tomcat script can use an environment variable named
TOMCAT_OPTS to set extra command line parameters to the JVM (such as memory
setting etc.). On UNIX® you can also create a file named ".tomcatrc" in your
home directory and Tomcat will take environment information such as PATH,
JAVA_HOME, TOMCAT_HOME and CLASSPATH from this file. On NT however (and also
on UNIX® when the modifications are for something such as the JVM command line)
you are forced to rewrite some of the startup script...
Do not hesitate, just do it.
Modify the default JVM settings
The default JVM settings in the tomcat script are very naove; everything is
left for defaults. There are a few things that you should consider to improve
your Tomcat performance:
- Modify your JVM memory configuration.
Normally the JVM allocates an initial size for the Java heap and that's it,
if you need more then this amount of memory you will not get it.
Nevertheless, in loaded sites, giving more memory to the JVM improves
Tomcat's performance. You should use command line parameters such as
-Xms/-Xmx/-ms/-mx to set the minimum/maximum size of the Java heap (and
check to see if the performance was improved).
- Modify your JVM threading configuration. The HP JDK1.2.x for Linux comes
with support for both, green and native threads. In general native threads
are known to provide improved performance for I/O bound applications, green
threads on the other hand put less stress on the machine. You should
experiment with these two threading models and see which model is better
for your site (in general, native threads are better).
- Select the best JVM for the task.
http://www.hp.com/products1/unix/java/
Modify your connectors
The Connectors, as configured in Tomcat's default server.xml contains
two Connectors configured as in the next server.xml fragment:
The two default Connectors in server.xml
<!-- (1) HTTP Connector for stand-alone operation -->
<Connector className="org.apache.tomcat.service.PoolTcpConnector">
<Parameter name="handler"
value="org.apache.tomcat.service.http.HttpConnectionHandler"/>
<Parameter name="port"
value="8080"/>
</Connector>
<!-- (2) AJPV12 Connector for out-of-process operation -->
<Connector className="org.apache.tomcat.service.PoolTcpConnector">
<Parameter name="handler"
value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/>
<Parameter name="port"
value="8007"/>
</Connector>
- Is a Connector that listens on port 8080 for incoming HTTP requests.
This connector is needed for stand-alone operation.
- Is a Connector that listens on port 8007 for incoming AJPV12 requests.
This connector is needed for web-server integration (out-of-process servlet
integration).
The AJPV12 Connector is required for Tomcat shutdown. However, the HTTP
Connector may be removed if stand-alone operation is not needed.
Use a thread pool in your connectors
Tomcat is a multi-threaded servlet container this means that each request
needs to be executed by some thread. Prior to Tomcat 3.2, the default was to
create a new thread to serve each request that arrives. This behavior is
problematic for loaded sites because:
- Starting and stopping a thread for every request puts a needless burden on
the operating system and the JVM.
- It is hard to limit the resource consumption. If 300 requests arrive
concurrently Tomcat will open 300 threads to serve them and allocate all the
resources needed to serve all the 300 requests at the same time. This causes
Tomcat to allocate much more resources (CPU, Memory, Descriptors...) than it
should and it can lead to low performance and even crashes if resources are
exhausted.
The solution for these problems is to use a thread pool, which is the default
for Tomcat 3.3. Servlet containers that are using a thread pool relieve
themselves from directly managing their threads. Instead of allocating new
threads; whenever they need a thread they ask for it from the pool, and when
they are done, the thread is returned to the pool. The thread pool can now be
used to implement sophisticated thread management techniques, such as:
- Keeping threads "open" and reusing them over and over again. This saves
the trouble associated with creating and destroying threads continuously.
- Usually the administrator can instruct the pool not to keep too many
idle threads, freeing them if needed.
- Setting an upper bound on the number of threads used concurrently. This
prevents the resources allocation problem associated with unlimited
thread allocation.
- If the container maxed out to the threads upper limit, and a new
request arrives, the new request will have to wait for some other
(previous) request to finish and free the thread used to service it.
You can refine the techniques described above in various ways, but these are
only refinements. The main contribution of thread pools is thread-reuse and
having a concurrency upper bound that limits resource usage.
Using a thread pool in Tomcat is a simple move; all you need to do is to use a
PoolTcpConnector in your <Connector> configuration. For example the following
server.xml fragment defines ajpv12, pooled Connector:
Pooled ajpv12 Connector
<!-- A pooled AJPV12 Connector for out-of-process operation -->
<Connector className="org.apache.tomcat.service.PoolTcpConnector">
<Parameter
name="handler"
value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/>
<Parameter
name="port"
value="8007"/>
</Connector>
This fragment is very simple and the (default) pool behaviour instructed
by it is:
- Upper bound for concurrency of 50 threads.
- When the pool has more then 25 threads standing idle it will start to
kill them.
- The pool will start 10 threads on creation, and it will try to keep 10
vacant threads (as long as the upper bound is kept).
The default configuration is suitable for medium load sites with an average
of 10-40 concurrent requests. If your site differs you should modify this
configuration (for example reduce the upper limit). Configuring the pool can
be done through the <Connector> element in server.xml as demonstrated in the
next fragment:
Configuring the thread pool
<!-- A pooled AJPV12 Connector for out-of-process operation -->
<Connector className="org.apache.tomcat.service.PoolTcpConnector">
<Parameter
name="handler"
value="org.apache.tomcat.service.connector.Ajp12ConnectionHandler"/>
<Parameter
name="port"
value="8007"/>
<Parameter
name="max_threads"
value="30"/>
<Parameter
name="max_spare_threads"
value="20"/>
<Parameter
name="min_spare_threads"
value="5" />
</Connector>
As can be seen the pool has 3 configuration parameters:
- max_threads - defines the upper bound to the for the concurrency, the pool
will not create more then this number of threads.
- max_spare_threads - defines the maximum number of threads that the pool will
keep idle. If the number of idle threads passes the value of
max_spare_threads the pool will kill these threads.
- min_spare_threads - the pool will try to make sure that at any time there
is at least this number of idle threads waiting for new requests to arrive.
min_spare_threads must be bigger then 0.
You should use the above parameters to adjust the pool behavior to your needs.
Disable servlet auto-reloading
Servlet auto-reloading is really useful for development time. However it is
very expensive (in performance degradation terms) and may put your application
in strange conflicts when classes that were loaded by a certain classloader
cannot co-operate with classes loaded by the current classloader.
So, unless you have a real need for class reloading during your
deployment you should turn off the reloadable flag in your contexts.
Authors
This document was created by:
Gal Shachor
With help from (alphabetical ordered):
Jonathan Bnayahu
Fiona Czuczman
Costin Manolache
Copyright (c)1999 The Apache Software Foundation
Legal Stuff They Make Us Say - http://jakarta.apache.org/legal.html
Contact Information - http://jakarta.apache.org/contact.html
Legal notices
Copyright (c) Hewlett-Packard Company 1997-2001. All Rights Reserved.
Reproduction, adaptation, or translation without prior written
permission is prohibited, except as allowed under the copyright laws.
UNIX® is a registered trademark in the United States and other countries,
licensed exclusively through X/Open Company Limited.
WHILE THE INFORMATION IN THIS PUBLICATION IS BELIEVED TO BE ACCURATE,
HEWLETT-PACKARD MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THIS
MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Hewlett-Packard
shall not be liable for errors contained herein or for incidental or
consequential damages in connection with the furnishing, performance
or use of this material. Information in this publication is subject
to change without notice.
Java and all Java-based trademarks and logos are trademarks or
registered trademarks of Sun Microsystems, Inc. in the U.S. and
other countries.
|