Question

Why is sessionDestroyed() called only on invalidate or timeout, but not called when the server is terminating? How I can make some operations on each session when server is terminating?

Was it helpful?

Solution

Regarding your first question:

Why sessionDestroyed() called only on invalidate or timeout, but not calling when server is terminating?

Here is an explanation of why sessionDestroyed() is not getting invoked when shutting down in Tomcat (at least as of Tomcat 6.0.33), from a post on Tomcat: http://comments.gmane.org/gmane.comp.jakarta.tomcat.user/215644

When I request my web application I receive sessionCreated, when I invalidate the session from the code I receive sessionDestroyed. Unfortunately when I stop the web application I do not receive sessionDestroyed.

I checked the code where StandardContext is stopped. I can see that StandardManager.stop is called which is ok But when StandardManager.doUnload() is invoked, in its implementation * session.expire(false)* is called where the "false" is actually the flag that indicates whether to notify the listeners or not. As it is invoked with "false" - the listeners are not invoked.

This is not my post, and I am attributing credit to Violeta, the author of the post.

The post provides a way to patch StandardManager.java.

If you did not want to modify that class (I personally avoid modifying classes that belong to the Application Server whenever possible), there are other approaches you could take.

Regarding your second question:

how I can make some operations on each session when server is terminating?

Why do you want to do operations on each session when the server is terminating? The Servlet specification provides methods for executing code when the server is being shutdown. However, they do not provide a means by which you can perform operations on each active Session (probably by design).

As this earlier answer mentions, How to access HTTP sessions in Java, Session management should be handled by the Servlet Container, and you may reconsider your current approach to your application.

For general handling cleanup when the server is being shutdown, you have ServletContextListener.contextDestroyed and Servlet.destroy

The ServletContextListener interface provides a contextDestroyed lifecycle method

The Servlet interface provides a destroy method, which is used to release any resources or handle any cleanup when the server is being shutdown.

OTHER TIPS

Java Servlet 3.0 specification states that the HttpSessionListener.sessionDestroyed() should be called upon server/application shutdown (section 11.3.4 Notifications At Shutdown):

On application shutdown, listeners are notified in reverse order to their declarations with notifications to session listeners preceding notifications to context listeners. Session listeners must be notified of session invalidations prior to context listeners being notified of application shutdown.

Tomcat 7 implements Java Servlet 3.0 specs and should support your use case.

Reading Execute code after Glassfish Web Deployment i came to the answer of this question.

We have the ability to code a ServletContextListener to be triggered when the context is loaded or terminated like this:

public class MyServlet implements ServletContextListener {

  public void contextInitialized(ServletContextEvent e) {
         // implementation code
  }

  public void contextDestroyed(ServletContextEvent e) {
         // implementation code
  }
}

Now in your case, where a shutdown will be trigered, then the contextDestroyed() method will be called.

Reference:

Thanks to Garis Suero

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top