The easiest way is probably to have a single listener that implements both the HttpSessionListener and HttpSessionActivationListener interfaces. This has a single count of active sessions.
In the sessionCreated() method, increment the count In the sessionDestroyed() method, decrement the count In the sessionDidActivate() method, increment the count In the sessionWillPassivate() method, decrement the count. This should eliminate the need to store anything in the session. Hope this helps Andy -----Original Message----- From: Christian Hauser [mailto:[EMAIL PROTECTED] Sent: 04 September 2003 10:01 To: Tomcat Users List Subject: Re: Counting active sessions if server restarts very often Shapira, Yoav wrote: > Howdy, > > >>Is there an other way to implement this? Maybe by saving the variable >>activeSessions to a session (which is restored when the server has >>restarted)? > > > This is not a bad idea, and might be the easiest way. A DB write on > shutdown/read on startup is also an option. To recapitulate: I want to display all active sessions (~ active users). I use HttpSessionListener and increment a static int field every time sessionCreated is called. Unfortunately the server is restarted very often so I'd like to remember the active sessions by putting them into the session. But how? Like this? public class SessionCounter implements HttpSessionListener { public synchronized void sessionCreated(HttpSessionEvent event) { Integer i = (Integer)event.getSession().getAttribute("session.counter"); if (i == null) { i = new Integer(0); } int activeSessions = i.intValue() + 1; event.getSession().setAttribute("session.counter", activeSessions); } public synchronized void sessionDestroyed(HttpSessionEvent event) { Integer i = (Integer)event.getSession().getAttribute("session.counter"); if (i == null) { i = new Integer(0); } int activeSessions = i.intValue(); if (activeSessions > 0) { activeSessions--; } event.getSession().setAttribute("session.counter", activeSessions); } public static int getActiveSessions() { return activeSessions; } } Jon Wingfield gave me the hint to put an object that implements HttpSessionActivationListener as an attribute to the session. But if I do that in the SessionListener#sessionCreated method I have 100 of those objects around when 100 concurrent users are using my web application. Does that make any sense? And what should I do when the object implementing HttpSessionActivationListener enters sessionWillPassivate? How do I save the count of active sessions? Sorry for all those questions, but I'd like to count the sessions even when the server restarts very often. Thank you for your help, Christian --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]