Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding a Timer to a ServletContext Attribute

 
Timy McTipperstan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What appserver and OS are you using?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben its Tomcat 5.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Havent looked at that one yet, will be looking into that this morning.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic