• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Counting active sessions of web application

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
I wrote a JSP and a Servlet some time ago that both count the active sessions of a web application and display them.
To do that I wrote a class SessionCounter which implements HttpSessionListener and thus is being informed when a new session is created or a session has been destroyed.
Unfortunately there seems to be a problem with my program, because the web application says that there are currently -16 active sessions.
Maybe there is a problem within my code or some resources within the servlet engine (ServletExec 4.1) are not reset when restarted.
I hope someone can give me a hint.
Thank you in advance,
Christian
Code of JSP (similar in Servlets in other web applications):

Here's the class SessionCounter, which belongs to a utility JAR that is situated in the lib directory of each web application.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your sessionDestroyed...
public void sessionDestroyed ( HttpSessionEvent se )
{
if ( activeSessions > 0 )
{
activeSessions--;
}
}
Your code continues to decrement even if activeSessions is a negative number.
onlyOOD
 
Christian Hauser
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okey, but why is it that I get negative numbers? Is this a problem of my ServletEngine or does it have to be this way?
Christian (a little confused about HttpSessionEvents)
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christian Hauser:
Okey, but why is it that I get negative numbers? Is this a problem of my ServletEngine or does it have to be this way?
Christian (a little confused about HttpSessionEvents)


what is the scope of the bean?
I remember tomcat having problems in invalidating sessions.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JAMon is a performance tuning API, but it also does much more than that. Here is some sample code using JAMon that I have successfully used to count http sessions. You can also use it to co unt the number of times any particular user logs in as well as if they are currently logged in. You can also tell maximum and average number of sessions and more. JAMon can also track hits, average max and min execution times per page and more.
Sample session and page hit tracking code can be found at
http://www.javaperformancetuning.com/tools/jamon/#SampleCode
JAMon documentation and the download can be found at http://www.jamonapi.com
Steve
 
Christian Hauser
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Steve for pointing me to your JAMon API. I'll have a look at it.
However, I still do not understand WHY it is possible that I get a negative session count. Why should there be more sessionDestroyed() calls than sessionCreated() (overall)?
Christian (still confused).
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the benefit of those not on the tomcat-user list...

Tomcat will restore an active session after restart, but your static variable will not. So if you have 20 active sessions and the server bounces, your sessions are re-created without the sessionCreated event being called.

When one of these sessions eventually gets destroyed, your counter will go negative.
 
Christian Hauser
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mike.
In fact, I was just about to start updating this thread when I read your post.
Christian
 
Water! People swim in water! Even tiny ads swim in water:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic