We help IT Professionals succeed at work.

Detect that tomcat as restored a session after stop / start

modsiw
modsiw asked
on
How can I detect that tomcat has restored a HttpSession from persistence?

I'm looking for similar behavior to HttpSessionListener that is triggered when a session is made.
Comment
Watch Question

Commented:
I think that  
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSessionActivationListener.html   
does what you want it to do.
It has the   sessionDidActivate   method.

Author

Commented:
I have the class below. It is in my web.xml as:

    <listener>
      <listener-class>com.floorsoft.floorwizard.server.state.SessionListener</listener-class>
    </listener>

sessionCreated is called when I log on, but if I stop / start the server, sessionWillPassivate / sessionDidActivate are not called

Any idea what I'm doing wrong?
package com.floorsoft.floorwizard.server.state;

import com.floorsoft.floorwizard.server.ServerConstants;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionActivationListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

// class is used by Tomcat
public class SessionListener implements HttpSessionActivationListener, HttpSessionListener
{
  public void sessionCreated(HttpSessionEvent hse)
  {
    final HttpSession hs = hse.getSession();
    final String sessionId = hs.getId();
System.out.println("binding session Created: " + sessionId);
    Monitor.mapSession.put(sessionId,hs);
  }

  public void sessionDestroyed(HttpSessionEvent hse)
  {
final HttpSession hs = hse.getSession();
final String sessionId = hs.getId();
System.out.println("unbinding session Destroyed: " + sessionId);
    Monitor.mapSession.remove(hse.getSession().getId());
  }

  public void sessionDidActivate(HttpSessionEvent hse)
  {
    final HttpSession hs = hse.getSession();
    final String sessionId = hs.getId();
System.out.println("binding session Activate: " + sessionId);
    Monitor.mapSession.put(sessionId,hs);
    try {
      Monitor m = (Monitor)hs.getAttribute(ServerConstants.attrMonitor);
      if (m != null) {
        if (m.personId != null)                       Monitor.mapLogon.get(m.personId).add(sessionId);
        if (m.projectWrite != null && m.projectWrite) Monitor.mapProject.put(m.projectId,sessionId);
        if (m.accountId != null)                      Monitor.mapAccount.get(m.accountId).add(sessionId);
      }
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void sessionWillPassivate(HttpSessionEvent hse)
  {
final HttpSession hs = hse.getSession();
final String sessionId = hs.getId();
System.out.println("unbinding session Passivate: " + sessionId);
    Monitor.mapSession.remove(hse.getSession().getId());
  }
}

Open in new window

Author

Commented:
Another note:

I don't have a tomcat cluster. It seems HttpSessionActivationListener was designed to service clusters. Could this be an issue?

Commented:
From  
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSession.html 

"When container migrates a session between VMs in a distributed container setting, all session attributes implementing the HttpSessionActivationListener interface are notified."  

I can't test either. So I don't where we can go with this.  
I googled,
http://www.xyzws.com/Servletfaq/when-do-i-use-httpsessionactivationlistener/4   
http://www.coderanch.com/t/170002/java-Web-Component-SCWCD/certification/HttpSessionActivationListener-configured-DD   

Author

Commented:
My tests and those links indicate that HttpSessionActivationListener won't solve my problem. :(

Commented:
>Detect that tomcat as restored a session after stop / start  
I don't think that ever happens.
Please tell us more about your problem.

Author

Commented:
When I log on to my site, attributes are added to my session and my session is added to a static collection.

If I stop / start the server then refresh my browser those attributes are still there which is evident because the site still knows that I am user xyz and have preferences abc which were set in the attributes of my session.

The collection is reinitialized with the server. If I view a list of logged on sessions which is taken from this collection, it is empty. I need some way to populate it with the HttpSessions that persisted.

Author

Commented:
The collection I mentioned in my preview comment is Monitor.mapSession from my example code above.

Author

Commented:
I figured out why my HttpSessionActivationListener method's weren't being called:

I was using it like HttpSessionListener. Eg, expecting tomcat to call it bc it is a registered listener.

It should be used like HttpSessionBindingListener and attached as an attribute to the sessions.

Commented:
Great, I am glad that you kept working and found the solution.  
>It should be used like HttpSessionBindingListener and attached as an attribute to the sessions.  
I guess the API( that I cited above) wasn't very clear.

Author

Commented:
The docs could be read either way, but there was a red herring.

It has the same event type as HttpSessionListener, which is not the same event type as HttpSessionBindingListener. So I subconsciously decided it behaved like HttpSessionListener without really questioning it.