• 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

context listener and

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am needing to hold a cache of data from the database, I load the data at the startup of my webapp and put in the application context via the contextListener, this is fine.
However what I want to do is periodocally check the database to see if this cache needs updating, I want this check to be automatic, eg every hour.
Has anyone got any ideas how to achieve this automatic update to an object on the application scope?
any ideas/opinions appriciated.

thanks

Stu.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try a scheduler (quartz is very easy if implemented with Spring). The scheduler will periodically refresh your data stored in the context.

Alternatively, if you have used a cache (eg ehcache) you can set the timeout for the cache as the refresh time.
 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestion.
I'll take a look at the quartz solution.
Alternatively how about if I start off a thread in the contextListener and leave it running in a loop?
 
Aditya Keyal
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may have to actually try this out because I am not too sure of this but for the infinite thread solution:

a) implement the Runnable interface
b) override the run() method and in that write your code in an infinite loop (i.e. while(true))

This should work but what would be the performance overhead is something we need to look into.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of creating your own thread that runs continuously, use the java.util.Timer and TimerTask classes. Those allow you to schedule tasks for periodic execution.
 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys,
I have used the Spring config and Quartz to prototype a solution.
thanks again for the suggestions

Stu.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic