When do I use HttpSessionActivationListener?
The HttpSessionActivationListener is used for responding to events when a sessions object migrates from one VM to another. The HttpSessionActivationListener only has any relevance when a session is part of a web application in a distributed environment. When a session is activated, notification that the session has just been activated will be received by the SessionDidActivate method and when the session is about to be destroyed, the sessionWillPassivate method is invoked.
Objects, that are bound (stored) to the session and its class implements HttpSessionActivationListener interface, can be initialised or destroyed based upon the listener. Also, when the session migrates between servers, the sessionWillPassivate is called and once it is successfully moved, the sessionDidActivate will be called. It is interesting to note that the sesssion is not yet ready for service at the time the sessionDidActivate method is called.
The specification mandates that a container may migrate a session to another JVM for purposes such as load balancing or fail-over. This type of capability is considered vital for the deployment of large scale web application.
The HttpSessionActivationListener has two methods:
public void sessionDidActivate(HttpSessionEvent se)
public void sessionWillPassivate(HttpSessionEvent se)
The following quote from Interface HttpSessionActivationListener API document:
Objects that are bound to a session may listen to container events notifying them that sessions will be passivated and that session will be activated. A container that migrates session between VMs or persists sessions is required to notify all attributes bound to sessions implementing HttpSessionActivationListener.
Notes:
- For an attributes to be migrated between distributed JVM's any attributes must implement the
java.io.Serializableinterface. - Don't need to (and must not) configure the classes that implements this interface in the deployment descriptor (web.xml), just make a class implementing the
HttpSessionActivationListenerinterface. - The JSP/Servlet standard does not require that a web container supports distributed applications, but it must support the HttpSessionActivationListener interface, to allow code to be capable of supporting such an environment.
Session passivation is where inactive sessions are written to a persistent store. Session activation is the ability to a previously passivated session into memory from the persistent store.
An example implements this interface:
public class myObject implements HttpSessionActivationListener {
...
public void sessionWillPassivate(HttpSessionEvent se) {
System.out.println("session is about to be passivated");
... //you can cleanup and store something into persistent storage
}
public void sessionDidActivate(HttpSessionEvent se) {
System.out.println("session has just been activated");
... //you can init and revtrive something from persistent storage
}
}
Most Recent servlet Faqs
- What is the getInputStream() of ServletRequest for?
- How to get the client information in a Servlet?
- Can I control Session timeout?
- What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?
- When do I use HttpSessionListener?
- What is the difference between the request attribute and request parameter?
- What is the defferent between getNamedDispatcher() and getRequestDispatcher()?
Most Viewed servlet Faqs
- What is the difference between the request attribute and request parameter?
- When do I use HttpSessionListener?
- How to use ServletContext.getResourceAsStream(java.lang.String path)?
- What is the difference between RequestDispatcher's forward method and HttpServletResponse's sendRedirect method?
- Can I control Session timeout?
- How to get the client information in a Servlet?
- What is the defferent between getNamedDispatcher() and getRequestDispatcher()?