本文整理汇总了Java中org.apache.commons.net.ntp.TimeInfo.getOffset方法的典型用法代码示例。如果您正苦于以下问题:Java TimeInfo.getOffset方法的具体用法?Java TimeInfo.getOffset怎么用?Java TimeInfo.getOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.net.ntp.TimeInfo
的用法示例。
在下文中一共展示了TimeInfo.getOffset方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOffset
import org.apache.commons.net.ntp.TimeInfo; //导入方法依赖的package包/类
private long getOffset(TimeInfo info) throws SystemTimeManagerException {
NtpV3Packet message = info.getMessage();
logStuff(info, message);
info.computeDetails(); // compute offset/delay if not already done
Long offsetValue = info.getOffset();
Long delayValue = info.getDelay();
log.info("Roundtrip delay=" + delayValue + "ms, clock offset="
+ offsetValue + "ms");
if (offsetValue == null) {
throw new SystemTimeManagerException(
"Could not get offset needed to adjust local clock to match remote clock");
} else {
return offsetValue;
}
}
示例2: updateOffSet
import org.apache.commons.net.ntp.TimeInfo; //导入方法依赖的package包/类
private static void updateOffSet()
{
//CREATE CLIENT
NTPUDPClient client = new NTPUDPClient();
//SET TIMEOUT
client.setDefaultTimeout(10000);
try
{
//OPEN CLIENT
client.open();
//GET INFO FROM NTP SERVER
InetAddress hostAddr = InetAddress.getByName(NTP_SERVER);
TimeInfo info = client.getTime(hostAddr);
info.computeDetails();
//UPDATE OFFSET
if(info.getOffset() != null)
{
offset = info.getOffset();
}
}
catch (Exception e)
{
//ERROR GETTING OFFSET
}
client.close();
}
示例3: queryServerNow
import org.apache.commons.net.ntp.TimeInfo; //导入方法依赖的package包/类
private void queryServerNow() {
Long offsetResult = null;
for (int i = 0; i < MAX_QUERY_RETRIES; i++) {
try {
NTPUDPClient client = new NTPUDPClient();
client.setDefaultTimeout(DEFAULT_NTP_TIMEOUT_MS);// Timeout if a response takes longer than 10 seconds
client.open();
InetAddress address = InetAddress.getByName(ntpServer);
TimeInfo info = client.getTime(address);
info.computeDetails();
Long offset = info.getOffset();
if (offset == null) {
throw new Exception("Could not calculate time offset (offset is null)");
} else {
offsetResult = offset;
break;
}
} catch (Exception e) {
log.error("Error querying NTP server, attempt {} of {}", (i + 1), MAX_QUERY_RETRIES, e);
}
}
if (offsetResult == null) {
log.error("Could not successfully query NTP server after " + MAX_QUERY_RETRIES + " tries");
throw new RuntimeException("Could not successfully query NTP server after " + MAX_QUERY_RETRIES + " tries");
}
lastOffsetGetTimeSystemMS = System.currentTimeMillis();
lastOffsetMilliseconds = offsetResult;
log.debug("Updated local time offset based on NTP server result. Offset = {}", lastOffsetMilliseconds);
}
示例4: getOffset
import org.apache.commons.net.ntp.TimeInfo; //导入方法依赖的package包/类
public static long getOffset(final TimeInfo info) {
final Long offsetValue = info.getOffset();
final Long delayValue = info.getDelay();
return (offsetValue!=null ? offsetValue.longValue() : 0)+(delayValue!=null ? delayValue.longValue() : 0);
}