my dog learned polymorphism*
The moose likes Servlets and the fly likes Servlet & Filter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet & Filter" Watch "Servlet & Filter" New topic
Author

Servlet & Filter

Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
HighLevel:
I have 3 filters configured for a servlet.I have to calculate average time taken by each filter.

Details:

1. I wrote a singleton object to maintain average times.

2. I am adding System.currentTime() in each filter doFilter() method start and end & at end setting these values to request.

3. In the servlet, I am adding these times & incrementing the requests count in the singleton object.

Till this point it is working properly.


But, In the first filter starting, I have to send this counters values by reading from singleton object to some monitoring logs.
Here is the problem.

Parellel requests causing inconsistent time values... Any ideas please???
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

My initial thought was to use the session id of the request to separate the requests, but a user with multiple tabs could still send simultaneous requests. You could try adding a UUID as an attribute to each request in the first Filter.


OCJP6, OCWCD5
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Think about different methods of storing time values, other than in an object, e.g database, text files, etc.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18710
    
    8

I don't see why parallel requests should cause a problem, unless perhaps your strategy was to pass this singleton object a starting timestamp and a finishing timestamp and ask it to calculate the difference.

If you had code in the filters which used local variables to calculate the running time and then pass the result to this singleton object, you would just need a tiny bit of synchronization in the singleton to remove the issue.
Mani Venkata Kanth
Ranch Hand

Joined: Aug 21, 2006
Posts: 39
Hi all, Thank you for your replies.

Paul, Yes... I am calculating start & end times in the filter and finally setting time diff to singleton. Yep, I did synchronization. It is hitting some performance issues.
Any more ideas please???

Paul Clapham wrote:I don't see why parallel requests should cause a problem, unless perhaps your strategy was to pass this singleton object a starting timestamp and a finishing timestamp and ask it to calculate the difference.

If you had code in the filters which used local variables to calculate the running time and then pass the result to this singleton object, you would just need a tiny bit of synchronization in the singleton to remove the issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet & Filter