How To Get An Accurate Session Count With Tomcat

By | July 15, 2008

Recently we started to notice that even after disabling a server in our loadbalancer we still get traffic on it after two months. At least for as far as we count it. We currenlty count traffic by using the SessionListener to get notification of people leaving and coming, but somehow our counter gets stuck at about 500 sessions 2 months after switching the server of in the load balancer.

Some time back I already wrote about the setup we use in counting sessions. Read more on that in the ‘Cleaning Up Java Sessions’ post. We put live this change some time back. But I’m still having some issues with the sessions.

What we do is something like this. When a user comes in through a servlet we look if a specific object is set in the session. If not we invalidate the session and add the object. In a seperate class we catch all session starts and destructions.

Upon the session start function in HttpSessionListener we increase the counter. Upon the destruction of the session and if the our session object is set we decrease the counter. But for some reason we have some sessions that do not get cleaned up by Tomcat. Or there are some users that simply stay online for over 2 months.

To get a semi accurate count of the active session I looked into getting the amount of active sessions from the Tomcat StandardManager class. In theory you would be able to obtain this through the tomcat implementation of the HttpSession object. But for some reason these objects are package protected.

There has to be an easier way of getting an accurate count of the amount of active sessions through the tomcat API.

2 thoughts on “How To Get An Accurate Session Count With Tomcat

  1. Jongerius Post author

    @Jayasankar: you can’t get the number of active users from tomcat out of the box. But like stated in the post you can do it sorta manually by tracking session start and end notifications using the HttpSessionListener.


Leave a Reply