@Override public void sampled(long nanosTimeStamp, List<ResourceInstance> resourceInstances) { synchronized (this) { if (this.enableMonitorThread) { final StatMonitorNotifier thread = this.notifier; if (thread != null) { try { thread.monitor(new MonitorTask(System.currentTimeMillis(), resourceInstances)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } else { monitor(System.currentTimeMillis(), resourceInstances); } } }
void start() { synchronized (this) { if (this.consumer == null) { this.consumer = new LoggingThread(toString(), this); this.alive = true; this.consumer.start(); } } }
@Override public void run() { final boolean isDebugEnabled_STATISTICS = logger.isTraceEnabled(LogMarker.STATISTICS_VERBOSE); if (isDebugEnabled_STATISTICS) { logger.trace(LogMarker.STATISTICS_VERBOSE, "StatMonitorNotifier is starting {}", this); } try { work(); } finally { synchronized (this) { this.alive = false; if (this.producer != null) { this.producer.interrupt(); } } } if (isDebugEnabled_STATISTICS) { logger.trace(LogMarker.STATISTICS_VERBOSE, "StatMonitorNotifier is stopping {}", this); } }
@Test public void testStatMonitorNotifierWakesUpForWork() throws Exception { StatMonitorHandler handler = new StatMonitorHandler(); TestStatisticsMonitor monitor = new TestStatisticsMonitor(); handler.addMonitor(monitor); final StatMonitorNotifier notifier = handler.getStatMonitorNotifier(); assertTrue(notifier.isAlive()); waitUntilWaiting(notifier); // if notification occurs then notifier woke up... assertEquals(0, monitor.getNotificationCount()); handler.sampled(NanoTimer.getTime(), Collections.<ResourceInstance>emptyList()); waitForNotificationCount(monitor, 1, 2 * 1000, 10, false); assertEquals(1, monitor.getNotificationCount()); // and goes back to waiting... waitUntilWaiting(notifier); }
@Test public void testStatMonitorNotifierAliveButWaiting() throws Exception { StatMonitorHandler handler = new StatMonitorHandler(); TestStatisticsMonitor monitor = new TestStatisticsMonitor(); handler.addMonitor(monitor); final StatMonitorNotifier notifier = handler.getStatMonitorNotifier(); assertTrue(notifier.isAlive()); waitUntilWaiting(notifier); for (int i = 0; i < 20; i++) { assertTrue(notifier.isWaiting()); Thread.sleep(10); } }
private void stopNotifier_IfEnabledAndRunning() { if (this.enableMonitorThread && this.notifier != null) { this.notifier.stop(); this.notifier = null; } }
private void startNotifier_IfEnabledAndNotRunning() { if (this.enableMonitorThread && this.notifier == null) { this.notifier = new StatMonitorNotifier(); this.notifier.start(); } }
private static void waitUntilWaiting(StatMonitorNotifier notifier) throws InterruptedException { boolean done = false; for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < 2000; done = (notifier.isWaiting())) { Thread.sleep(10); } assertTrue("waiting for notifier to be waiting", done); }