aspose file tools*
The moose likes Servlets and the fly likes Adding a Timer to a ServletContext Attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Adding a Timer to a ServletContext Attribute" Watch "Adding a Timer to a ServletContext Attribute" New topic
Author

Adding a Timer to a ServletContext Attribute

Timy McTipperstan
Ranch Hand

Joined: Feb 17, 2005
Posts: 32
I have a servlet that on startup compiles a list of 700 people. It has become apparent that a more regular/frequent schedule needs to be established. So I'm looking into running it once an hour. How would I implement scheduleAtFixedRate in that type of situation.


[ March 25, 2005: Message edited by: Timy McTipperstan ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What appserver and OS are you using?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61241
    
  66

You really don't want to as there are better ways to do this.

This is a typical caching pattern. Here's a better solution:

1) Create a 'people list' class that the rest of the app interacts with to obtain the list.

2) This cache class is instantiated at app startup and stored in application scope.

3) Whether it faults in the list of people at startup is up to you.

4) Whenever someone requests the list of people, this class checks to see if the list needs to be updated first. This could include critera such as:

a) list hasn't been populated yet
b) list is over an hour old
c) whatever other criteria you require

Be sure to attend to synchronization while the list is being updated.
[ March 25, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
You can achieve it in the following ways
1) subscribe to a timmer event which will notify your people compiling process every hours?
2) keep track of your last refesh time. If the currenttime - last refresh time > 1hr, recompile your people list.
3) create a thread and put it to sleep for an hour
Timy McTipperstan
Ranch Hand

Joined: Feb 17, 2005
Posts: 32
Ben its Tomcat 5.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Tomcat doesn't have any built in timers. Resin has one but it goes beyond the servlet spec (might not matter to you).

I also asked about the OS because the easiest way to implement this is with a cron job and wget. With one line of code, you can have the OS make a request to your servet every [i]n[/n] days, minutes, weeks, etc...
There are probably equivalents in Windows but I'm not sure what they are.

Also, James Curwen (sp?) posted the code to a timer he wrote within the last month or so. I'm not sure if it was here or in the JSP forum.

Did you not like Bear's caching pattern?
Timy McTipperstan
Ranch Hand

Joined: Feb 17, 2005
Posts: 32
Havent looked at that one yet, will be looking into that this morning.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding a Timer to a ServletContext Attribute