How can I access NTP clock in java
Asked Answered
H

2

8

I have a distributed program written in java. I want my nodes access a synchronized physical clock.

I know NTP is a protocol for physical clock synchronization. I know that I can install it on linux by sudo apt-get ntp.

My question is when I install it, how can I access this synchronized clock in my java program? I mean what happens when I install ntp on my machine? my system clock will be sync?

Thanks :)

Heartland answered 2/8, 2016 at 4:22 Comment(2)
Yes, your system clock is synchronized, and Java uses the system clock.Strobel
I check this scenario: In two different servers, I installed ntp using sudo apt-get install ntp. Then from one server I read System.currentTimeMillis() and send it to the other server. The other server reads its own time using currentTimeMillis and prints the different. The difference is higher than 97 seconds! I am sure something is wrong. Do you know which part?Heartland
N
12

If want to access NTP info in Java you can create UDP packet conforming to the NTP packet format (NTP RFC-1305) setting mode field to MODE_CLIENT (3) then send the packet to NTP server on port 123 and listen for a response.

The Apache Commons Net library already has the framework to do this using only a few lines of code.

 NTPUDPClient client = new NTPUDPClient();
 client.open();
 InetAddress hostAddr = InetAddress.getByName("*insert-target-server-host-name.com*");
 TimeInfo info = client.getTime(hostAddr);
 info.computeDetails(); // compute offset/delay if not already done
 Long offsetValue = info.getOffset();
 Long delayValue = info.getDelay();
 String delay = (delayValue == null) ? "N/A" : delayValue.toString();
 String offset = (offsetValue == null) ? "N/A" : offsetValue.toString();

 System.out.println(" Roundtrip delay(ms)=" + delay
                + ", clock offset(ms)=" + offset); // offset in ms
 client.close();

Note that the local clock offset (or time drift) is calculated with respect to the local clock and the NTP server's clock according to this standard NTP equation.

LocalClockOffset = ((ReceiveTimestamp - OriginateTimestamp) +
                             (TransmitTimestamp - DestinationTimestamp)) / 2

Where OriginateTimestamp (t1) is the local time the client sent the packet, ReceiveTimestamp(t2) is time request received by NTP server, TransmitTimestamp (t3) is time reply sent by server, and DestinationTimestamp (t4) is time at which reply received by client on local machine.

See client example for full code:
https://commons.apache.org/proper/commons-net/examples/ntp/NTPClient.java

Notify answered 3/9, 2016 at 17:30 Comment(0)
B
8

When you setup ntp, System time will be synchronized with the ntp server time. When you use System.currentTimeMillis() will have value of the automatic adjusted system clock.

You should be aware that Timer can be sensitive to changes in the system clock, ScheduledThreadPoolExecutor isn't. You might check Scheduler Impacts with clock changes

Burglarize answered 2/8, 2016 at 5:11 Comment(4)
Thank you very much. I have a related question too: what happens if ntp server dies? then all programs that have been written and rely on ntp server will break?! I would prefer a way that synchronized the clock of my nodes only. I don't care about real actual time, and I don't care about the rest of the world. I just want my own nodes to by synchronized. Is there any way for that?Heartland
NTP servers are usually are very highly available, one potential problem might be the network link to the NTP server. You can have very simple sync option : Assuming one of the your node is master from which you want to sync the time, and the master is running ssh. You could simply retrieve the current time from it using following command over ssh (you need to configure passwordless ssh between slave nodes and master nodes). date --set="$(ssh user@server date)"Burglarize
Calling this command one time will set the date of my machine to be sync with master, or I need to run the command whenever I want to retrieve the clock?Heartland
Based on your use case, you can run as scheduled job (like using cron job) or one demand.Burglarize

© 2022 - 2024 — McMap. All rights reserved.