| Author |
Free resources when session times out
|
Param Yanamandra
Ranch Hand
Joined: Jun 06, 2002
Posts: 33
|
|
Hi Everyone, I need some help on something I m working on. I am developing a web application using Struts and Tomcat 5.0.28 as Web Server. When the User logs into the application I logon to another resource and I maintain a connection to that resource which I make sure to terminate when the user clicks on the Logout button. This is taken care of in the LogoutAction class developed using Struts. However, there are instances where the user might not click logout and the Web Server times out the session. In that case the resources are still being used up since the user never clicked on the Logout and the LogoutAction class never got invoked Is there a way I could make sure that the Logout Action class gets invoked if the session times out or is there a location (similar to finalize method in Garbage collection) where I can place the Logout specific code that would free up the resources. Your thoughts on this would be appreciated. Thanks in advance, Param Yanamandra
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 23635
|
|
|
Yes, you can implement a session listener. The listener's sessionDestroyed() gets called when the sesion gets invalidated or times out. There you can call your Action.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Certs: SCEA Part 1, Part 2 & 3 & Core Spring 3, OCAJP
|
 |
Param Yanamandra
Ranch Hand
Joined: Jun 06, 2002
Posts: 33
|
|
|
Thanks for your quick reply. I tried to do it this way. I made my LogoutAction class implement HttpSessionListener and wasn't successful. Is there a problem with doing it that way.
|
 |
Param Yanamandra
Ranch Hand
Joined: Jun 06, 2002
Posts: 33
|
|
I should have added in my earlier message that I added this to the web.xml of my application <listener> <listener-class>LogoutAction</listener-class> </listener>
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 23635
|
|
If LogoutAction is in a package, you need to use the fully qualified name. For example com.mycompany.LogoutAction. Also, can you post the sessionCreated() and sessionDestroyed() methods.
|
 |
Param Yanamandra
Ranch Hand
Joined: Jun 06, 2002
Posts: 33
|
|
Hi Jeanne, Thanks for your response. It worked. I had a typo in giving the complete path of the Action class. Appreciate your help. Param
|
 |
Marc Peabody
pie sneak
Sheriff
Joined: Feb 05, 2003
Posts: 4695
|
|
I'm very curious after reading this thread. What is the advantage of setting up such a listener? By my understanding, if the session is destroyed the objects within it lose their references which leaves them open for garbage collection. If you "cleanup" the session(I assume it is meant that all object references are set to null) then we've removed references to the actual objects directly leaving them [again] open for garbage collection. Is there something I am missing?
|
A good workman is known by his tools.
|
 |
Param Yanamandra
Ranch Hand
Joined: Jun 06, 2002
Posts: 33
|
|
Hi Marc, That is a good question. I realized it minutes after posting my earlier response. The one suggested by Jeanne earlier will do any clean up after the session has been destroyed. I overlooked it while reading the API docs. I realized a while later that eventhough my method has been called it wasn't freeing up the resources since the Session has timed out. I think what I really need to implement is the HttpSessionBindingListener interface and free up my resources using the public void valueUnbound(HttpSessionBindingEvent event) method. This method gets called just before the session is being timed out and I can put my code for freeing up resources in this method. Haven't tried it out yet. Please let me know what you guys think about this. Thanks, Param
|
 |
Marc Peabody
pie sneak
Sheriff
Joined: Feb 05, 2003
Posts: 4695
|
|
I do not think you addressed my question. What exactly are you cleaning up? What advantages are you expecting to get out of this?
|
 |
Param Yanamandra
Ranch Hand
Joined: Jun 06, 2002
Posts: 33
|
|
Hi, When I log into my application I also log into some devices by creating Session to that device. The max. sessions that can be opened to these devices is very low and the sessions have a very high timeout value (24hrs). For other users to be use these devices these sessions have to be explicitly cleaned or wait till they time out. So in my application when I logout I make sure I clean up these sessions. I m not sure how these sessions(to devices) are handled when a session(Tomcat session) times out and garbage collector removes it. So I always want to make sure that the clean up is done irrespective of how the Tomcat session terminates so that I don't reach the max. session limit on these devices. The clean up is what I put in the valueUnBound method. I hope it makes sense to you. Please feel free to let me know if I m not clear enough. Thanks, Param
|
 |
Marc Peabody
pie sneak
Sheriff
Joined: Feb 05, 2003
Posts: 4695
|
|
Ok, so you do not want to rely on the server for letting go of the references ASAP. I can understand that. Thanks for replying.
|
 |
 |
|
|
subject: Free resources when session times out
|
|
|