Class NetTimeConnector<C extends NetTimeConfiguration>
- Type Parameters:
C- generic configuration type
- All Implemented Interfaces:
TimeSource<Moment>
- Direct Known Subclasses:
DaytimeClock,HttpClock,SntpConnector
Represents an abstract connection object to an internet time server.
A socket connection can be established by connect() and then the difference
between the local computer clock using SystemClock.MONOTONIC and the net time
can be queried. Next time queries of this clock are based on this difference
and the local clock - until the next CONNECT.
- Author:
- Meno Hochschild
- See Also:
SystemClock.MONOTONIC
-
Method Summary
Modifier and TypeMethodDescriptionvoidconnect()Queries a time server for the current time.Yields the current time after a connection has been established at least once.Yields the time of last connection.longYields the last offset between net time and local time in microseconds.Yields the current configuration parameters.booleanDetermines if the internal logging is enabled.booleanThe clock is running as soon as there was established a connection at least once.booleanQueries the configuration parameters to be used for the next connection.voidsetLogWriter(PrintWriter out)Installs a logging stream for any messages during connection.Methods inherited from class net.time4j.clock.AbstractClock
inLocalView, inPlatformView, inZonalView, inZonalViewMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.time4j.base.TimeSource
currentInstant
-
Method Details
-
currentTime
Yields the current time after a connection has been established at least once.
If there was no connection yet then this method just displays the time of clock construction. Note that this method is still sensible for any local clock change triggered by the underlying operating system. If an application needs a fresh internet time then following code can be used instead (causing network traffic):
NetTimeConnector<?> clock = ...; clock.connect(); Moment currentTime = clock.getLastConnectionTime();
- Returns:
- Moment
- See Also:
isRunning()
-
isRunning
public boolean isRunning()The clock is running as soon as there was established a connection at least once.
If there is not yet any connection then this clock will only display the time of its construction.
- Returns:
- boolean
- See Also:
connect()
-
connect
Queries a time server for the current time.
The result can then be achieved by the method
currentTime()which is based on the network offset calculated in last connection. A connection to the server only happens in this method and not in the methodcurrentTime().- Throws:
IOException- if connection fails or in case of any inconsistent server answers- See Also:
currentTime()
-
reconfigure
public final boolean reconfigure()Queries the configuration parameters to be used for the next connection.
The configuration will be determined by a
ServiceLoader. If not available then this connector will just choose the configuration which was given during construction of this instance. Any possibleIllegalStateExceptionwill be logged to the error console. This method delegates toloadNetTimeConfiguration().- Returns:
trueif successful elsefalse- See Also:
loadNetTimeConfiguration()
-
getNetTimeConfiguration
Yields the current configuration parameters.
- Returns:
- configuration object
- See Also:
reconfigure()
-
setLogWriter
Installs a logging stream for any messages during connection.
- Parameters:
out- output stream (nulldisables logging)
-
isLogEnabled
public boolean isLogEnabled()Determines if the internal logging is enabled.
- Returns:
trueif there is any installed log writer elsefalse- See Also:
setLogWriter(PrintWriter)
-
getLastConnectionTime
Yields the time of last connection.
- Returns:
- moment of last connection or
nullif there was not any connection yet - See Also:
connect()
-
getLastOffsetInMicros
public long getLastOffsetInMicros()Yields the last offset between net time and local time in microseconds.
- Returns:
- offset in microseconds (
0if there was not any connection yet) - See Also:
connect()
-