Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Map object with some special features.

 
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.
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to Javaranch
 
Ranch Hand
Posts: 160
IntelliJ IDE VI Editor Ubuntu
  • 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
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic