Inaccurate clock new gets an mbean to control it

This commit is contained in:
Alex Huang 2011-07-25 16:01:31 -07:00
parent c610925304
commit 9c627a15f3
2 changed files with 72 additions and 36 deletions

View File

@ -187,6 +187,10 @@ public class AgentMonitor extends Thread implements Listener {
}
}
if (agentsBehind.size() > 0) {
s_logger.info("Found the following agents behind on ping: " + agentsBehind);
}
return agentsBehind;
}

View File

@ -17,44 +17,76 @@
*/
package com.cloud.utils.time;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.management.StandardMBean;
import org.apache.log4j.Logger;
import com.cloud.utils.concurrency.NamedThreadFactory;
import com.cloud.utils.mgmt.JmxUtil;
/**
* This clock is only accurate at a second basis.
*
* This clock is only accurate at a second basis, which is useful for most applications.
*/
public class InaccurateClock extends Thread {
static final InaccurateClock s_timer = new InaccurateClock();
static {
time = System.currentTimeMillis();
s_timer.start();
}
private static long time;
protected InaccurateClock() {
super("InaccurateClock");
}
@Override
public void run() {
while (true) {
public class InaccurateClock extends StandardMBean implements InaccurateClockMBean {
private static final Logger s_logger = Logger.getLogger(InaccurateClock.class);
static ScheduledExecutorService s_executor = null;
static final InaccurateClock s_timer = new InaccurateClock();
private static long time;
public InaccurateClock() {
super(InaccurateClockMBean.class, false);
time = System.currentTimeMillis();
restart();
try {
JmxUtil.registerMBean("InaccurateClock", "InaccurateClock", this);
} catch (Exception e) {
s_logger.warn("Unable to initialize inaccurate clock", e);
}
}
@Override
public synchronized String restart() {
turnOff();
s_executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("InaccurateClock"));
s_executor.schedule(new SetTimeTask(), 60, TimeUnit.SECONDS);
return "Restarted";
}
@Override public String turnOff() {
if (s_executor != null) {
try {
s_executor.shutdown();
} catch (Throwable th) {
s_logger.error("Unable to shutdown the Executor", th);
return "Unable to turn off check logs";
}
}
s_executor = null;
return "Off";
}
public static long getTime() {
return s_executor != null ? time : System.currentTimeMillis();
}
public static long getTimeInSeconds() {
return time / 1000;
}
protected class SetTimeTask implements Runnable {
@Override public void run() {
try {
time = System.currentTimeMillis();
Thread.sleep(1000);
} catch(Exception e) {
}
}
}
public static long getTime() {
if (s_timer.isAlive()) {
return time;
} else {
return System.currentTimeMillis();
}
}
public static long getTimeInSeconds() {
// This is obviously not accurate because it >> 10 is / 1024 but it's close enough since we're inaccurate.
return getTime() >> 10;
}
} catch (Throwable th) {
try {
s_logger.error("Unable to time", th);
} catch (Throwable th2) {
}
}
}
}
}