2

I know NTP servers can be used to synchronize your computer's system clock. But can NTP be used by an application that wants to schedule things in sync with other systems?

Scenario: Developing a java app (perhaps to run in an ESB like Mule) and you won't necessarily be able to control the time of the machine on which it will run. Can your app use an NTP server to obtain the time and schedule tasks to run based on that time?

Let's say you're using Quartz as the scheduler and perhaps joda-time for handling times (if that's useful). The time doesn't have to be super precise, just want to make sure not ahead of remote systems by much.

Cincinnati Joe
  • 2,077
  • 6
  • 23
  • 32

3 Answers3

2

If you're not super worried about drift, and assuming that the machines aren't just randomly changing time, then you could ping an NTP server to get what time IT thinks it is, and compare that to the time your local machine thinks that it is, then calculate the differential and finally schedule your task in local time.

So, for example, say that the NTP server says that it's 12:30, but your local machine says that it is 12:25. And you want your task to go off at 13:00 NTP time.

So, 12:25 - 12:30 = -0:05. 13:00 + (-0:05) = 12:55, therefore you schedule your task for 12:55.

Addenda --

I can't speak to the naivety of an implementation, I'm not familiar enough with the protocol.

In the end it comes down to what level of practical accuracy is acceptable to you. NTP is used to synchronize time between systems. One of the problems it solves is by being continually invoked, it prevents clock creep. If you use the "NTP Ping, schedule with offset" technique, and, say, that future time is perhaps 8 hrs in the future, there's a very real possibility of clock creep, meaning that although you wanted the task to go off at "12:55", when 12:55 rolls around, it could be off from the original NTP server since the clocks have not been synced (at all), and the job has not been rescheduled to virtually resync.

Obviously, the longer the period between original schedule and actual execution, the more the potential for drift. This is an artifact no matter how good the original NTP ping is. If you do not plan on rescheduling these tasks as they get close to execution time in order to compensate for drift, then odds are any "reasonable" implementation of NTP will suit.

There's the Apache Commons NET library that has a NTP client. Some complain that it uses System.currentTimeMillis(), which has (had?) resolution issues (10-15ms) on Windows. System.nanoTime addresses this, and you could easily change the library to use that, and rebuild it.

I can't speak to how it reflects the "naivety" of the implementation. But in the end it comes down to how close you need to keep the two machines and their jobs (virtually) in sync.

Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • Right. So what's a good way to "ping an NTP server to get what time IT thinks it is"? I've come across the old "naive" [Java SNTP Client](http://support.ntp.org/bin/view/Support/JavaSntpClient) but thought there might be something else (ideally with Joda time though I can sort that part out). – Cincinnati Joe Apr 03 '12 at 16:27
  • I looked around and found a newer java library: [AtomicDate](http://atomicdate.sourceforge.net/) I haven't pulled it down to look at its docs yet. – Cincinnati Joe Apr 03 '12 at 17:49
0

But can NTP be used by an application that wants to schedule things in sync with other systems?

I've never heard of it being used that way. However, there's nothing to stop you implementing a client for the Network Time Protocol (RFC 1305). A full NTP implementation is probably overkill, but you can also use the protocol in SNTP mode (RFC 2030).

You probably want to set up and use a local NTP server if you want high availability and reasonable accuracy.

A Google search indicates that there are a number of Java NTP clients out there ...

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Yeah, but the top hit doesn't appear to have been updated since 2004. Here's a related StackOverflow question: [Java NTP client](http://stackoverflow.com/questions/925191/java-ntp-client) – Cincinnati Joe Apr 03 '12 at 16:25
0

My intuition tells me that the NTP requires a hardware clock adjustments to keep a pace. So if you don't have access to the hardware, you cannot do it.

However, if it is enough to have a few seconds precision, you could periodically send sample time from a server to calculate a skew between the system clock and adjust scheduled time for jobs.

kan
  • 28,279
  • 7
  • 71
  • 101