本文整理汇总了Java中org.apache.commons.net.ntp.NTPUDPClient类的典型用法代码示例。如果您正苦于以下问题:Java NTPUDPClient类的具体用法?Java NTPUDPClient怎么用?Java NTPUDPClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NTPUDPClient类属于org.apache.commons.net.ntp包,在下文中一共展示了NTPUDPClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initTimeComponents
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
private void initTimeComponents() {
final NTPUDPClient ntpUDPClient = new NTPUDPClient();
final NTPFetch ntpFetch = new NTPFetch(ntpUDPClient, pluginConfig);
final NTPSynchTask ntpSynchTask = new NTPSynchTask(ntpFetch, pluginConfig);
final TimeWatch timeWatch = new TimeWatch(clock);
final NTPProvider ntpProvider = new NTPProvider(ntpSynchTask,
timeWatch,
pluginConfig);
final TickTimeFetch tickTimeFetch = new TickTimeFetch(tickQuoteRepository);
final TickTimeProvider tickTimeProvider = new TickTimeProvider(tickTimeFetch, timeWatch);
serverTimeProvider = new ServerTimeProvider(ntpProvider, tickTimeProvider);
final DummyMessageHandler dummyMessageHandler = new DummyMessageHandler();
final DummySubmitRunner dummySubmitRunner = new DummySubmitRunner(orderUtil, dummyMessageHandler);
final DummySubmit dummySubmit = new DummySubmit(dummySubmitRunner, pluginConfig);
final MarketState marketState = new MarketState(dataService, dummySubmit);
brokerTime = new BrokerTime(client,
serverTimeProvider,
marketState);
}
示例2: getCurrentTime
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
private long getCurrentTime() {
long currentTime = 0;
NTPUDPClient client = new NTPUDPClient();
client.setDefaultTimeout(WAIT_FOR_SERVER_RESPONSE);
try {
client.open();
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MMM dd yyyy HH:mm:ss.SSS zzz");
for (String server : NTP_SERVERS) {
try {
InetAddress ioe = InetAddress.getByName(server);
TimeInfo info = client.getTime(ioe);
TimeStamp ntpTime = TimeStamp.getNtpTime(info.getReturnTime());
return ntpTime.getTime();
} catch (Exception e2) {
System.out.println("Can't get response from server: " + server + ".");
}
}
} catch (SocketException se) {
System.out.println("Can't open client session");
} finally {
client.close();
}
return currentTime;
}
示例3: getNTPDate
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
public static String getNTPDate() {
String[] hosts = new String[] { "ntp02.oal.ul.pt", "ntp04.oal.ul.pt",
"ntp.xs4all.nl", "time.foo.com", "time.nist.gov" };
NTPUDPClient client = new NTPUDPClient();
// We want to timeout if a response takes longer than 5 seconds
client.setDefaultTimeout(2000);
SimpleDateFormat OutPutFormat = new SimpleDateFormat(
"dd/M/yyyy HH:mm:ss", java.util.Locale.getDefault());
for (String host : hosts) {
try {
InetAddress hostAddr = InetAddress.getByName(host);
TimeInfo info = client.getTime(hostAddr);
Date date = new Date(info.getReturnTime());
String out = OutPutFormat.format(date);
return out;
} catch (IOException e) {
e.printStackTrace();
}
}
client.close();
return null;
}
示例4: getNtpServerDate
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
/**
* Queries the specified NTP Server for the time.
* Do not query time server more than once every 4 seconds.
* @param timeServerHost the time server host name.
* @return the NTP server {@link Date} or {@code null}.
* @throws IOException
*/
public static Date getNtpServerDate(final String timeServerHost) throws IOException {
try {
TimeInfo timeInfo = null;
final NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_SERVER_TIMEOUT_MS);
final InetAddress inetAddress = InetAddress.getByName(timeServerHost);
if (inetAddress != null) {
timeInfo = timeClient.getTime(inetAddress);
if (timeInfo != null) {
// TODO: which time to use?
final long serverTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
//long serverTime = timeInfo.getReturnTime();
final Date ntpDate = new Date(serverTime);
return ntpDate;
}
}
} catch (final IOException e) {
throw new IOException("Unable to get NTP server time.", e);
}
return null;
}
示例5: getTime
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
/**
* Queries the given timeserver <code>hostname</code> and returns the time
* in milliseconds.
*
* @param hostname the timeserver to query
* @return the time in milliseconds or the current time of the system if an
* error occurs.
*/
protected static long getTime(String hostname) {
try {
NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_TIMEOUT);
InetAddress inetAddress = InetAddress.getByName(hostname);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
return timeInfo.getReturnTime();
}
catch (UnknownHostException uhe) {
logger.warn("the given hostname '{}' of the timeserver is unknown -> returning current sytem time instead", hostname);
}
catch (IOException ioe) {
logger.warn("couldn't establish network connection [host '{}'] -> returning current sytem time instead", hostname);
}
return System.currentTimeMillis();
}
示例6: getCurrentNetworkTime
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
public long getCurrentNetworkTime() throws IOException{
NTPUDPClient timeClient = new NTPUDPClient();
TimeInfo timeInfo = null;
InetAddress inetAddress = InetAddress.getByName(Constants.TIME_SERVER);
timeInfo = timeClient.getTime(inetAddress);
//long returnTime = timeInfo.getReturnTime(); //local device time
long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime(); //server time
timeInfo.getMessage().getTransmitTimeStamp().getTime();
Date time = new Date(returnTime);
//TODO make this into a debug mode/verbose?
if(true){
CharSequence text = "Time from " + Constants.TIME_SERVER + ": " + time +
timeInfo.getMessage().getTransmitTimeStamp().toDateString();
Log.d("ntp", text.toString());
}
return returnTime;
}
示例7: get
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
public static TimeInfo get() throws IOException {
final NTPUDPClient client = new NTPUDPClient();
client.setDefaultTimeout(10000);
client.open();
final InetAddress hostAddr = InetAddress.getByName(EEWBot.instance.getConfig().getNptServer());
return client.getTime(hostAddr);
}
示例8: NTPClock
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
public NTPClock(String name,
ZoneId zoneId,
List<String> ntpHosts,
NTPUDPClient client,
long pollInterval,
int resolution) {
this(name, zoneId, ntpHosts, client, pollInterval, resolution, null);
}
示例9: activate
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
@Activate
protected void activate(ComponentContext ctx) throws SocketException {
client = new NTPUDPClient();
String serversList = (String) ctx.getProperties().get("ntp.servers");
setServers(serversList.split(","));
setSocketTimeout();
}
示例10: NTPServerTimeProvider
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
/**
* Creates NTPServerTimeProvider with a custom list of NTP server addresses
*
* @param ntpServers Array of custom NTP server addresses
* @throws UnknownHostException Throws UnknownHostException for the first unresolved host, if no hosts were resolvable
*/
public NTPServerTimeProvider(String[] ntpServers) throws UnknownHostException, SocketException {
client = new NTPUDPClient();
setServers(ntpServers);
if (servers.isEmpty()) {
throw new UnknownHostException(ntpServers[0]);
}
setSocketTimeout();
}
示例11: updateOffSet
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的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();
}
示例12: main
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("Usage: NTPClient <hostname-or-address-list>");
System.exit(1);
}
NTPUDPClient client = new NTPUDPClient();
// We want to timeout if a response takes longer than 10 seconds
client.setDefaultTimeout(10000);
try {
client.open();
for (String arg : args) {
System.out.println();
try {
InetAddress hostAddr = InetAddress.getByName(arg);
System.out.println("> " + hostAddr.getHostName() + "/"
+ hostAddr.getHostAddress());
TimeInfo info = client.getTime(hostAddr);
processResponse(info);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
} catch (SocketException e) {
e.printStackTrace();
}
client.close();
}
示例13: queryServerNow
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的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);
}
示例14: getTime
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
/**
* Queries the given timeserver <code>hostname</code> and returns the time
* in milliseconds.
*
* @param hostname the timeserver to query
* @return the time in milliseconds or the current time of the system if an
* error occurs.
*/
protected static long getTime(String hostname) {
try {
NTPUDPClient timeClient = new NTPUDPClient();
timeClient.setDefaultTimeout(NTP_TIMEOUT);
InetAddress inetAddress = InetAddress.getByName(hostname);
TimeInfo timeInfo = timeClient.getTime(inetAddress);
return timeInfo.getReturnTime();
} catch (UnknownHostException uhe) {
logger.warn("the given hostname '{}' of the timeserver is unknown -> returning current sytem time instead",
hostname);
} catch (IOException ioe) {
logger.warn("couldn't establish network connection [host '{}'] -> returning current sytem time instead",
hostname);
}
return System.currentTimeMillis();
}
示例15: NTPFetch
import org.apache.commons.net.ntp.NTPUDPClient; //导入依赖的package包/类
public NTPFetch(final NTPUDPClient ntpUDPClient,
final PluginConfig pluginConfig) {
this.ntpUDPClient = ntpUDPClient;
fetch = fromURL(pluginConfig.ntpServerURL());
}