• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Free resources when session times out

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author & internet detective
Posts: 42011
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Param Yanamandra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 42011
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Param Yanamandra
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic