• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design Question - Refreshing Singletons

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic