wood burning stoves 2.0*
The moose likes Servlets and the fly likes context listener and Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "context listener and " Watch "context listener and " New topic
Author

context listener and

Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
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

Joined: Dec 01, 2008
Posts: 71
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.


- Aditya Webservices Blog
Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
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

Joined: Dec 01, 2008
Posts: 71
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
Marshal

Joined: Mar 22, 2005
Posts: 41068
    
  43
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.


Ping & DNS - my free Android networking tools app
Stu Johns
Ranch Hand

Joined: Aug 11, 2008
Posts: 63
thanks guys,
I have used the Spring config and Quartz to prototype a solution.
thanks again for the suggestions

Stu.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: context listener and
 
Similar Threads
Data Caching Ideas
Possible to clear or disable JBoss3/EJB2 cache?
cache
any ideas for implementing the lockout
Help Me Design a Caching Solution