This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
The app I'm working on has a singleton class that needs to be refreshed every so often. Does anyone have any design ideas on how I can achieve this?
The singleton contains a hashmap which reflect data that hardly gets changed in our database. But, if the data does change, we want to refresh hash map without redeploying app. Maybe a timer(??) not sure how to do this.
Any one out there have any ideas... Examples would be great.
Well, as far as the singleton class itself is concerned, a synchronized method that refreshes its data would take care of that. Either a method you call on the single instance itself, or a static method that just creates a new single instance. Depends on how the data refresh works.
But your question seems to have hints of "How do I tell when data changes?" or something like that. Would you like to try expanding on that part of the question?
Joined: Apr 17, 2003
I guess I do not know how to handle the refresh itself. I know the appropriate code on how to refresh(ie., create new instance of singleton or synchronize on hash map and recreate it with new data).
I would like to refresh like once a day...or even more often...but I do not know how to do this?
I don't think there is a way to detect if the data changed in the db so I am opting on maybe refreshing every...hour, day. Is there a way to put a timer in singleton???
We had the same problem in our app, one of the solution is to refresh HashMap periodically, say every day, or every week. But there may be a situation that no update happend for 2 months, so updating everyday doesnt make sense. To overcome this situation we provided a "Refresh Cache" Link to Admin/Infrastructure user who would first update DB and then log in to application and click on "Refresh Cache" in the application. That ways he is in full control of the situation.
To keep your current Singleton, move whatever code loads up all the state in the Singleton from a constructor to an init() method. Then you can call the init() method any time you like. I'd expect it to create a new HashMap and populate it from scratch. During the brief time it's reloading, the map will be invalid for any clients. You may have to synchronize all access to the map.
Can you build your cache to "lazy load" so it knows how to load data that it doesn't have? With this you only have to empty the HashMap and let it reload itself over time. It makes the window of invalid time very short.
Finally, Singleton may not be needed here. You can get by with all static methods. I made one like this:
I called setTheRealCache in startup, avoided Singleton, gave clients the convenience of static methods, and allowed myself to plug in alternative RealCache implementations as needed. For example, one implementation has extra instrumentation, another has a different self cleaning algorithm.
The timer is one way to trigger your refresh. There are several algorithms for self-cleaning, like least recently used and time to live, which might help avoid the timer thread. And I always make sure to have a dashboard or admin console so I can clear caches on demand any time. [ May 21, 2007: Message edited by: Stan James ]
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