• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map object with some special features.

 
Madhiarasan Ramakrishnan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a collection map (user defined map) which should automatically removes the old objects which are older than 5 secs ( which should be specified dynamically).

I have a MDB to read a message from the queue and put it into hashmap, there is a another client program which constantly looking for this hashmap for the particular key.

sometime the client program may get timed out before it reads the key from hashmap,
in that situation the key entry should be deleted automatically by the user defined object after some point of time.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhiarasan Ramakrishnan wrote:a MDB

A what? A little Googling helped me found out you meant a Message Driven Bean, but my first guess question when I read this thread was "what does an MS Access database have to do with reading messages from a queue?". Next time, please don't use acronyms that aren't 100% clear to everybody.

As for the problem, have you searched around? Have you thought of a way to solve this yourself? I think I've seen an implementation for this once but I can't remember where.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to Javaranch
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My solution might not be the best, but I think I'd do it by extending HashMap, and using a helper class that contains a value and a timeAdded field as values in the extended HashMap class. I'd override the put() method to add instances of the helper class, and override the get() method to return the value of the helper class mapped to a particular key. That way, it's completely abstracted to the developer. Lastly, you could just have a timer which periodically loops through all of the values in your maps and remove those which were added more than 5 seconds ago. (Hint: Use System.currentTimeMillis())
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that's one way. I wouldn't use this map directly though; it will have issues with values() and entrySet() as these will contain the helper classes. You could use that map inside a wrapper similar to those of Collections.synchronizedMap and Collections.unmodifiableMap.

Another issue is when to remove entries. You can use a timer like you suggested. Another option is to remove on demand. The get method would look a bit like this then:
Of course entries may need to be invalidated when using several other methods as well. Think of methods like containsKey and containsValue, and the iterators over the keySet(), values() and entrySet() collections.

So the timer is probably the easiest solution if you can at least keep synchronization issues at bay.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic