Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

context listener and

 
Stu Johns
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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.
 
Aditya Keyal
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic