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]



Reply via email to