Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes In-memory hashmap refresh in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "In-memory hashmap refresh in servlet" Watch "In-memory hashmap refresh in servlet" New topic
Author

In-memory hashmap refresh in servlet

Shishir Prasad
Greenhorn

Joined: Jan 14, 2009
Posts: 6
Hi,

I have written a servlet that creates a in-memory hashmap in the init() method by getting some data from the server.The servlet works fine except for one small problem.When some new entries get added in the DB then my servlet is not showing the latest entries.

One method would be to shift the hashmap creation logic out of the init() method but then each time the hashmap would get re-created and that would take time.I want the hashmap to be created inside the init() method so that the response time for the user is less.

I want that the hashmap should be updated after every 30 minutes without affecting the response time of user.Can anyone suggest something in this regard?

Thanks.
srinivas srinivasmeenavalli
Ranch Hand

Joined: Jul 13, 2008
Posts: 65


One method would be to shift the hashmap creation logic out of the init() method but then each time the hashmap would get re-created and that would take time.I want the hashmap to be created inside the init() method so that the response time for the user is less.

Let init method have initial hash map creation( what ever you followed). Hash map recreation would be placed in
either doGet or doPost methods.
I want that the hashmap should be updated after every 30 minutes without affecting the response time of user.Can anyone suggest something in this regard?

keep the timestamp/time in session.Retrive session value and calculate difference between current time and
time in session ,make a database hit to get latest values if difference exceeding more than 30 mins.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I would handle this by means of a separate Thread with low priority, started in the servlet init method.

Every X minutes it would create a new Hashmap. Note that it is important to leave the existing hashmap in place until the new one is completely ready. Replacing the reference with the new map will be atomic so requests will always have a hashmap to work with. Setting the priority low will avoid a response time hit.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In-memory hashmap refresh in servlet
 
Similar Threads
Infinite loop in Init method of Servlet
Loading a file in a servlet at run time
DB connection from servlets
Which one gets first executed ?????????
event firing in tomcat