This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Clock Synchronization in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Clock Synchronization in Java" Watch "Clock Synchronization in Java" New topic
Author

Clock Synchronization in Java

Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
I have a client sever system and need to synchronize the clocks. The constraints as as follows:
  • The server is an Weblogic EJB server. We currently communicate using RMI calls on the EJBs and through JMS.
  • The clients are running JDK 1.4 on windows (XP, 2000, and possibly other flavors).
  • The clients must be uniform within a tolerance of no more than 2 seconds, ideally 1 second or less. (This means no two client clocks can be more then 2 seconds apart).
  • Uniform lag between all clients and the server is acceptable, although we would prefer to keep this under 2 seconds as well.


  • The system is designed to work on college LAN, and hence has high bandwidth. It may also be used on remote college campuses, in which case there may be significantly longer ping times. (I'm willing to settle for a LAN only solution for now, if need be.)
    I'm willing to settle (although its not preferable) for non-Java solutions, so long as they can be integrated into the Java program. For example, if we installed some non-Java code on the server and all client which kept the clients in sync, and then the Java program could call this code to get the current time, that would be acceptable.
    I would also prefer an off-the-shelf solution because this just got shifted into a high priority item.
    Thanks in advance for your help.
    --Mark
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Well, the simplest solution is probably for all the clients to get the time from the same (single) source, either the server or some external third-party time server. Having everyone get the time from your sever may increase traffic, but they shouldn't have to do it very often, right? You don't need the server's time every time you need a time - you just need to measure the difference between the client system's time, and server time.
    I guess the biggest problem I see is what if the network/server is really busy. If there's a delay in receiving the time info, how do you know it's accurate? You can meaure how much time elapsed between the initial request and receiving a response; if that difference is no more than 4 seconds, then by splitting the difference and assuming the server time was measured in the middle of that period, you can ensure that your error is no greater than 2 seconds:

    Here I assume timeServer is an instance of a Remote object which sits on the server, returning a long for that machine's System.currentTimeMillis().


    "I'm not back." - Bill Harding, Twister
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    You could also bypass the server entirely and have clients connect to an NTP server somewhere.
    Mark Herschberg
    Sheriff

    Joined: Dec 04, 2000
    Posts: 6037
    Thanks for the replies Jim.
    Yeah, from my research, I'm probably going to run NetTime on the server which can run as a service to which clients can connect (effectively making it an NTP server). I'll then run Automachron on the clients, and just have the solution external to the system. (I need it to run on all flavors of windows--NetTime doesn't work on XP.)
    I considered the simple "ping" but wondered if there was something more robust, since the network/traffic can vary throughout the system, and I don't want to then effective write my own SNTP protocol. I was also mostly curious if there was some prefab Java code out there.
    --Mark
     
    Don't get me started about those stupid light bulbs.
     
    subject: Clock Synchronization in Java