Saturday, October 04, 2008

Request Arrival time tracking with Tomcat

This is something I really wanted but I did not find that so after doing some amount of work I found a way to track the request arrival time of Tomcat. By default it records request depart time and service time. There are some instances having that is not enough , so I did the following and get the job done.

- Implement the lifecycle interface that Tomcat has
- Then add a Value component in Tomcat (server.xml)

So class would be something like below , of course it is very simple one , need to modified but it just does the job.

public class TimeTracker
extends ValveBase
implements Lifecycle {

protected LifecycleSupport lifecycle = new LifecycleSupport(this);
private boolean started = false;

public ResourceTrackingAccessLogValve() {

public void invoke(Request request, Response response)
throws IOException, ServletException {

// Pass this request on to the next valve in our pipeline
long t1 = System.nanoTime();
getNext().invoke(request, response);
long t2 = System.nanoTime();
long time = t2 - t1;
System.out.println("ArriveTime ns " + t1 + " Departtime ns " + t2 + " ServiceTime : " + time);


public void addLifecycleListener(LifecycleListener listener) {



public LifecycleListener[] findLifecycleListeners() {

return lifecycle.findLifecycleListeners();


public void removeLifecycleListener(LifecycleListener listener) {



public void start() throws LifecycleException {
// Validate and update our current component state
if (started)
throw new LifecycleException
lifecycle.fireLifecycleEvent(START_EVENT, null);
started = true;

public void stop() throws LifecycleException {

// Validate and update our current component state
if (!started)
throw new LifecycleException
lifecycle.fireLifecycleEvent(STOP_EVENT, null);
started = false;



And then need to add the following Value tag in server.xml

<Valve className="org.log.ResourceTrackingAccessLogValve"
directory="logs" prefix="localhost_resource_log."
pattern="%a %t %D %s %b %U %q %m" resolveHosts="false"/>


Anura Guruge said...

I own

My mother's side of the family.

I don't have the time to do anything with it and none of the Jayasinghe's in the family are interested in it.

Do you want it?

Plus, IF you bother to BLOG why the HECK don't you provide DECENT contact information. Are you afraid?

Deepal Jayasinghe said...

Well, I have enough information on the blog to contact me :) , not only that if you just search the google you would find enough information about me.

I have read and enjoyed some of your books.