Can you suggest me how to solve this issue ? I have a singleton class and within that class I have a static hashtable which acts as a cache by containing all my dropdown list values. I also have a button in my app which reloads this hashtable incase any new dropdown values are added to the database. The problem is that now when my application is being deployed.. there are going to be 2 webservers and a load balancer inbetween. My question is if I click on the reload data button mentioned above, that will only reload data in the hash on one webserver and not the other. How should I resolve this issue and what is the general practice ? Tks a lot!
There are distributed cache products. I think a pay one is by Tangasol. There may be open source ones.
There are a few roll your own approaches too.
1) an admin screen in both web apps that you click on 'refresh cache'. 2) Check a database table on a timer. If the table has a 'refresh cache' date after the last time the cache was refreshed then refresh the cache. 3) Have an ejb or web service that you call and this service calls 'refresh' cache on both of your web apps. This could be a webservice or submission of an http page.
There are several ways to broadcast messages like "clear your cache now" among the cluster. JMS in pub-sub mode would seem the most natural. The "reload" button wouldn't clear a cache at all, but send a "clear cache" message. Each server in the cluster - including the one that sent the message - would get the message and clear the cache.
I worked with a vendor framework that sent messages across the cluster through a proprietary socket scheme. Their standard messages included startup, shutdown, refresh, and query status. They allowed other messages with a String argument so I sent messages like "/clearcache/cachename/". The receiver looks up "clearcache" in a map to get a classname, creates an instance and calls execute( commandString ).
Have you worked with JMS? There's a bit of a learning curve but it's not bad and this kind of thing may be very useful over the life of the system.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
If you just have 2 servers and not much worried about exact timing, you can follow this approach.
1. Have a servlet deployed in both the servers. 2. Whenever the hashtable is refreshed, send an intimation to the other servlet (ObjectOutputStream with hastable values or just some event to get it refreshed). 3. Servlet takes care of either updating the cache or refreshing it from DB.
Joined: Oct 19, 2005
Thank you all for the reply. Sorry, if I was not clear. Let me explain again. I have one web app deployed on 2 servers.. so 2 copies of the web app on the 2 servers. There is a reload button in my admin page which calls a servlet which contains the code to reload the hashmap. Now my issue is this servlet will execute on whatever server the loadbalancer sends the request to.. what should I do about the other server ? How do I call the servlet there ? Tks
Joined: Jun 26, 2002
A number of the responses answered your question. jms/web service/ejb/db/open source cache product/commercial cache product could all do what you want.