• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Freeing resources (threads) in web app

Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a web application which runs several threads. These threads are initialized in a class which is instantiated by a servlet and then put into servlet session when a user logs on the system.
When the user logs out I want to stop these threads. I can do this by getting the class instance from the session and calling a method which stops the threads.
But what can I do when the user closes his browser without logging out? How can I stop running threads when the session is closed (timeout)? Is there any method (like in EJB) which allows to norify a bean that it should free its resources?
The only idea I've got is to define a finalize method in that class. But that would stop these threads later when the instance is garbage collected and not when the session is invalidates.
Does anyone know any better solution of this problem?
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe if you create a class that implements the SessionListener interface. This interface defines 2 methods:
public void sessionCreated(HttpSessionEvent e)
public void sessionDestroyed(HttpSessionEvent e)
sessionDestroyed will be called whenever a session times out or HttpSession.invalidate() is called. However I'm not sure if the HttpSession object is still valid when the function is called.
I have to go to lunch now but I'll look into it when I come back
Hope this helps
Maciej Kolodziej
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dominic - your post helped me to find an answer. I haven't tested it yet, but I guess the best way is to implement javax.servlet.http.HttpSessionBindingListener. The javax.servlet.http.HttpSession javadoc says:
"When an application stores an object in or removes an object from a session, the session checks whether the object implements HttpSessionBindingListener. If it does, the servlet notifies the object that it has been bound to or unbound from the session."
Posts: 3892
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of curiosity -- why are you starting all these threads? What are they doing? This seems like an inefficient way to do things...
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic