File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Design Question - Refreshing Singletons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Design Question - Refreshing Singletons" Watch "Design Question - Refreshing Singletons" New topic
Author

Design Question - Refreshing Singletons

Annie McCall
Greenhorn

Joined: Apr 17, 2003
Posts: 21
Hi,

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.

Thanks.


Annie McCall<br />SCJP 1.4<br />SCWCD
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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?
Annie McCall
Greenhorn

Joined: Apr 17, 2003
Posts: 21
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???

Example code is appreciated.

Thanks.
[ May 18, 2007: Message edited by: Annie McCall ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Something like this:That's java.util.Timer if you didn't already find it in the documentation.
[ May 18, 2007: Message edited by: Paul Clapham ]
Ravish Ahuja
Greenhorn

Joined: Nov 29, 2004
Posts: 13
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.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design Question - Refreshing Singletons