jQuery in Action, 3rd edition
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes WeakHashmap or TimerTask  for stale sessions cleanup ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "WeakHashmap or TimerTask  for stale sessions cleanup ?" Watch "WeakHashmap or TimerTask  for stale sessions cleanup ?" New topic

WeakHashmap or TimerTask for stale sessions cleanup ?

steve mcdonald
Ranch Hand

Joined: Nov 18, 2005
Posts: 46
Well, the issue is about stale sessions.

Stale sessions - As we have the Weakhashmap data structure in jdk that cleans up unreferenced objects, though there is no regular specific time interval when it cleans up.
I looked at its implementation, it seem to clean up upon certain events such as calling size(), adding new entry etc., on the map.

What are the advantages of using this as opposed to timer. one less thing to do and addressing infinite locks by non-existing user sessions at the same time

Any comments ?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11776

Hi Steve,

My personal view is that the instructions seem to indicate that I should be able to own a lock as long as I want. There is no indication of a timeout in the interface definition. There is no indication of a timeout anywhere else in the instructions.

So if you were to implement a timeout, you are introducing something that has not been asked for. Furthermore you will be specifying a time limit that other users may not be expecting, and there is no standard idea of what sort of timeout to put on it - in real life a decision on timeout values would either come about after discussion with the relevant parties (which we cant do) or be configurable (hmmm - we could end up with too many configurable options in our GUI if we are not careful).

So, if we are going to worry about stale sessions (which I don't believe we have to worry about), then I would prefer to do it in one of two ways:
  • Have some generic way of clearing stale locks within the Data class itself that will work no matter whether we have a Sockets based or an RMI based solution. WeakHashMap fits that description.
  • Have a protocol based solution (clean-up code in SocketException in a sockets based solution, or in Unreferenced.unreferenced() for an RMI based solution.
  • Both have their good and bad points. Sometimes the interface you have been provided by Sun makes one or the other problematic which can be the deciding factor

    Regards, Andrew

    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    I agree. Here's the link: http://aspose.com/file-tools
    subject: WeakHashmap or TimerTask for stale sessions cleanup ?
    It's not a secret anymore!