public class SntpConnector extends NetTimeConnector<SntpConfiguration>
Connects to a modern time server using the NTP-protocol.
This class needs a socket connection via the port 123. The exact configuration can be set in the constructors but can also be changed at runtime. For safer protection against arbitrary local clock adjustments which might affect this clock, applications should also consider setting the system property "net.time4j.systemclock.nanoTime" to the value "true".
| Constructor and Description |
|---|
SntpConnector()
Creates a new instance which is configured by a
ServiceLoader. |
SntpConnector(SntpConfiguration ntc)
Creates a new instance which is configured by given argument.
|
SntpConnector(String server)
Creates a new instance which uses a default configuration
using the specified NTP4-server.
|
| Modifier and Type | Method and Description |
|---|---|
long |
currentTimeInMicros()
Returns the current time in microseconds since the Unix epoch
[1970-01-01T00:00:00,000000Z].
|
long |
currentTimeInMillis()
Returns the current time in milliseconds since the Unix epoch
[1970-01-01T00:00:00,000Z].
|
protected Moment |
doConnect()
Will be called by
connect(). |
protected Class<SntpConfiguration> |
getConfigurationType()
Yields the configuration type.
|
SntpMessage |
getLastReply()
Returns the last received message of the NTP-server.
|
protected SntpConfiguration |
loadNetTimeConfiguration()
Loads the configuration parameters via a
ServiceLoader. |
connect, currentTime, getLastConnectionTime, getLastOffsetInMicros, getNetTimeConfiguration, isLogEnabled, isRunning, log, reconfigure, setLogWriterinLocalView, inZonalView, inZonalViewpublic SntpConnector()
Creates a new instance which is configured by a
ServiceLoader.
IllegalStateException - if no configuration could be foundServiceLoaderpublic SntpConnector(SntpConfiguration ntc)
Creates a new instance which is configured by given argument.
ntc - SNTP-configurationpublic SntpConnector(String server)
Creates a new instance which uses a default configuration using the specified NTP4-server.
Example:
SntpConnector clock = new SntpConnector("ptbtime1.ptb.de");
clock.connect();
System.out.println(clock.currentTime());
server - NTP4-serverpublic long currentTimeInMillis()
Returns the current time in milliseconds since the Unix epoch [1970-01-01T00:00:00,000Z].
UTC leap seconds are never counted.
public long currentTimeInMicros()
Returns the current time in microseconds since the Unix epoch [1970-01-01T00:00:00,000000Z].
UTC leap seconds are never counted.
public SntpMessage getLastReply()
Returns the last received message of the NTP-server.
null if not yet receivedNetTimeConnector.connect()protected Moment doConnect() throws IOException
NetTimeConnectorWill be called by connect().
doConnect in class NetTimeConnector<SntpConfiguration>IOException - in case of any connection failureNetTimeConnector.connect()protected SntpConfiguration loadNetTimeConfiguration()
NetTimeConnectorLoads the configuration parameters via a ServiceLoader.
Subclasses should always start by calling
super.loadNetTimeConfiguration() if this
method is overridden.
loadNetTimeConfiguration in class NetTimeConnector<SntpConfiguration>NetTimeConnector.reconfigure()protected Class<SntpConfiguration> getConfigurationType()
NetTimeConnectorYields the configuration type.
getConfigurationType in class NetTimeConnector<SntpConfiguration>Copyright © 2014–2015. All rights reserved.