• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing a fn every hour

 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to run a java program that calls a function defined in it's class every hour. I've never done such a thing (only META refresh in HTML, hahaha).
Could anyone point me how to do this? I would run the java program from the command line and the logic to do the calling would reside in the main().
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a suggestion:

I would recommend starting a separate thread rather than keeping everything in main().
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marilyn!
Clarification: Is it necessary to call the function before the try {...} block? Is the difference simply:
function() ... 1 hour ... function() versus
...1 hour... function()?
 
Matt Siegel
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the java.util.Timer and the corresponding java.util.TimerTask classes. They will do exactly what you want.
HTH,
Matt
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Duran:
Clarification: Is it necessary to call the function before the try {...} block? Is the difference simply:
function() ... 1 hour ... function() versus
...1 hour... function()?

Yes. That's the only difference.
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matt, the Timer class looks like it would do the job but this code is going into an RMI server app which already extends UnicastRemoteObject. Since Timer requires a TimerTask object as one of the parameters to it's schedule functions that would mean I'd have to extend TimerTask (since it's abstract) within this class.
Since I can't extend 2 classes I guess I can't use TimerTask and Timer (unless I'm mistaken). Right now I'm able to implement threads because I'm implementing Runnable as opposed to extending Thread.
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
try javax.swing.Timer
it takes an actionListener so no need to extend, jsut implement the interface.
k
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic