views:

187

answers:

2

I need a method to be called in an App Server (Glassfish) every 5 seconds, no matter what. Are timers reliable enough for this? What are my options.

A: 

Unless you are on a real time system no "timer" is going to gve you that accuracy. Timers in general provide a "it will happen in at least the time you want... but maybe later" sort of thing.

This is an underlying OS thing, not a Java thing.

Edit:

That being said... do you really mean "5 seconds exactly" or will a few milliseconds later work? If a few milliseconds later works then a timer is fine. It is possible that sometimes that "few milliseconds" could be several hundred however, depending in what is going on in the system (os as well as things like garbage collection in the VM).

TofuBeer
s/minutes/seconds/
Ryan Graham
reading fail :-) fixing answer - thx
TofuBeer
A: 

Yes look at using the quartz api, its the only free enterprise class scheduling api for java that I know about. http://www.opensymphony.com/quartz/

I am currently using it in a large clustered financial application. The docs are a nightmare but its generally very reliable. Its a reliable are your going to get.

karl

Karl