Sunday, March 15, 2009

Struts - Accessing Value Stack in interceptors

Values which are set in action classes can be accessed and modified in interceptors
using the struts value stack object

Code in interceptor

ValueStack valueStack = actionInvocation.getStack();

Object object = valueStack.findValue(objectName);

if (object instanceof List) {

can access here directly and modify it which will be reflected in the viewing JSP


This method of accessing of modifying the Struts value stack will have wide applications.

Tomcat Connectors

Typically Communication protocol between apache and tomcat is AJP

Apache used mod_jk to speak AJP(Apache Jserv Protocol) to tomcat .

AJP is a binary packet based protocol - hence difficult to debug and limitation on packet size

  • AJP on tomcat side

Java Connector(BIO) - Blocking IO , one thread per connection

APR Connector - Allows more connections , allows polling

Java NIO Connector

  • Tomcats Http Connectors - can replace AJP specific features ,more options , more control ,Finely tuned,easy to debug since its text based

Nio , APR Connectors, BIO - fastest when not using persistent connections

  • mod_proxy_http - for HTTP - native protocol level support for authentication and SSL

  • HTTP Connector -

o.a.coyote.http11.Http11Protocol (BIO)

o.a.coyote.http11.Http11AprProtocol (APR)

o.a.coyote.http11.Http11NioProtocol (NIO)

HTTP/1.1 aliased to Http11 and Http11Apr depending on PATH or LD_LIBRARY_PATH settings

  • AJP Connector -



AJP/1.3 aliased to the two above
Depending on PATH or LD_LIBRARY_PATH settings

  • Use BIO if

Stability is the highest priority APR and NIO are more recent
Most content is dynamic
Keep alive is not a determining factor


  • Use APR if

SSL is terminated at Tomcat
Platforms are Linux or Windows
Keep alive is important
Lots of static content
Using Comet feature


  • Use NIO if

Compiling APR is not an option
SSL is terminated at Tomcat
Keep alive is important
Lots of static content
Using Comet features


  • requirement connectors in preference order

Stability --> Bio, APR/NIO


Low Concurrency --> BIO,APR,NIO

High Concurreny with keepAlive --> BIO,APR,NIO

Hig Concurrency with no keep alive -->APR,NIO,BIO

  • The native APR connector is unstable on Solaris

Performance Optimization for Tomcat

1. Clear the webapp directory in tomcat ( unnecessary loading of example and non essential applications)

2. Remove Duplicate Logging in Tomcat which comes by default
.handlers =, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler can be removed as logging is a costly operation

3. Using Asynchronous logging .
Tomcat by default using synchronous logging ,we can use log4j to change the default behavior

Synchronous logging ( Can become a bottleneck
We don’t want disk IO to become the limiting factor

4. The following should be considered for choosing the connectors

BIO stablity, static content offloaded into other server only dynamic content is present , keep alive non essential

APR ssl terminating at tomcat (because of openssl library it uses ),keep alive imp , also servers static content

NIO ssl terminating at tomcat, keepalive imp, static content
(keep alive becomes because SSL connection is costly )

check out separate blog on Tomcat Connectors ...

5. Best practice

Maintain a separate setenv.bat file which will be automatically called by tomcat 6 , which has custom settings(also Vm params).
This helps in easy migration

Use for server.xml substitution variables

6. Avoid using the log() methods in the Servlet API

7. Apache log4j

– in /WEB-INF/classes
– log4j-x.y.z.jar in /WEB-INF/lib

no seperate initializations required

8 . Have overflow protection for logs using cronolog

Overflow protection ( Size based rotation using JVM logger

9. In highly concurrent environments turn off KeepAlive

If its on , the socket stays connected and waits for the given time interval

10. Tuning Parameters

Maximum nr of concurrentrequests
For BIO, max nr of open/active connections

Typical values 1, 100-200
Represents the number of requests Tomcat will handle on a TCP connection
Set to 1 disables keep alive
connectionTimeout/keepAliveTimeout controls the timeout in between requests

Set to 1 if Very high concurrency


Represents nr of additional connections the OS should accept on your behalf
Useful when Tomcat can’t accept connections fast enough


Represents the SO_TIMEOUT value
Essentially, max time between TCP packets during a blocking read or write
Critical to a stable system
Also used for keep alive timeout

11. Enable caching for Static conetnt , will help in faster access from disk

Configured in element
•40MB cache (default 10MB)
•cache revalidation every 60 seconds (default 5 seconds)
•caching enabled (default true)

12. GC Settings –JDK 1.5 and 1.6


stops unauthorized shutdown of servers

Performance Optimization in Apache

1. Useful modules which could be used

mod_dbd -> Connection Pooling for other modules

mod_filter -> Dynamic chaing of filters, filters inserted based on request headers and response

mod_dumpio --> Dumps all IO to error log - in case of SSL dumping is done right before encryption and after decryption

mod_log_forensic --> helps in tracking request response , resulting in 2 lines of log for each request

mod_info - helps in giving info about all modules installed


Enabling can improve the performance by reducing IO calls for fetching the content (since all static content are served from apache)

mod_cache --> mod_disk_cache /mod_mem_cache - determines cache implementataion

3. Rewrite rules

a. Since Rewrite rules and regular expression matching are expensive can be replaced with Proxy pass where ever possible

RewriteRule ^/ws/(.*) http://localhost:8080/ws/$1 [P,L] with
ProxyPass ^/ws/ http://localhost:8080/ws/$1 [P,L]

b.Smart order of rewrite rules

The order of rewrite rules needs to be arranged in such a way that the maximum occurring instances should be available at the top , since the entire list is scanned trough for every request


RewriteRule ^/(.*\.jsp) http://localhost:8080/$1 [P]
RewriteRule ^/(.*\.do) http://localhost:8080/$1 [P]

Could be replaced with

RewriteRule ^/(.*\.do) http://localhost:8080/$1 [P]
RewriteRule ^/(.*\.jsp) http://localhost:8080/$1 [P]

Since the maximum number of requests we expect will be .do requests. This will improve performance because there will be only few jsp requests and most of the times execution can stop in the above line.

4. Also other parameters for Operating systems and GC tuning will enhance the performance

5. Graceful shutdown - handle existing request , time limit can be specified in case of long time
consuming request with property GracefulShutdownTime

6. Prefork MPM- Stable but not scalable - one process per request

Worker MPM(Preferred) - more scalable , thread safeness of libs critical - one thread per request

7. Useful Parametres

Controling Idle pool - MinspareServers /MaxspareServers




8. KeepAlives - prevents connections (socket open close for each request)

9.Avoid DNS Lookups

10. On the Fly Compression - deflate - bandwidth reduction and faster response times

Free Domain Names @!