Java library class to handle scheduled execution of "callbacks"?

Posted by Hanno Fietz on Stack Overflow See other posts from Stack Overflow or by Hanno Fietz
Published on 2009-03-05T10:29:45Z Indexed on 2010/04/07 6:53 UTC
Read the original article Hit count: 280

My program has a component - dubbed the Scheduler - that lets other components register points in time at which they want to be called back. This should work much like the Unix cron service, i. e. you tell the Scheduler "notify me at ten minutes past every full hour".

I realize there are no real callbacks in Java.

Here's my approach, is there a library which already does this stuff? Feel free to suggest improvements, too.

Register call to Scheduler passes:

  • a time specification containing hour, minute, second, year month, dom, dow, where each item may be unspecified, meaning "execute it every hour / minute etc." (just like crontabs)
  • an object containing data that will tell the calling object what to do when it is notified by the Scheduler. The Scheduler does not process this data, just stores it and passes it back upon notification.
  • a reference to the calling object

Upon startup, or after a new registration request, the Scheduler starts with a Calendar object of the current system time and checks if there are any entries in the database that match this point in time. If there are, they are executed and the process starts over. If there aren't, the time in the Calendar object is incremented by one second and the entreis are rechecked. This repeats until there is one entry or more that match(es). (Discrete Event Simulation)

The Scheduler will then remember that timestamp, sleep and wake every second to check if it is already there. If it happens to wake up and the time has already passed, it starts over, likewise if the time has come and the jobs have been executed.


Edit: Thanks for pointing me to Quartz. I'm looking for something much smaller, however.

© Stack Overflow or respective owner

Related posts about java

Related posts about cron