public T receive() { lock.lock(); try { while (true) { DelayedMessage message = queue.peek(); if (message == null && stopping) { return null; } if (message == null) { condition.await(); continue; } long now = timeProvider.getCurrentTime(); if (message.dispatchTime > now) { condition.awaitUntil(new Date(message.dispatchTime)); } else { queue.poll(); if (queue.isEmpty()) { condition.signalAll(); } return message.message; } } } catch (InterruptedException e) { throw UncheckedException.throwAsUncheckedException(e); } finally { lock.unlock(); } }
private void log(LogLevel logLevel, Throwable throwable, String message) { LogEvent logEvent = new LogEvent(timeProvider.getCurrentTime(), name, logLevel, message, throwable); OutputEventListener outputEventListener = context.getOutputEventListener(); try { outputEventListener.onOutput(logEvent); } catch (Throwable e) { // fall back to standard out e.printStackTrace(System.out); } }
/** Dispatches the given message after the given delay. */ public void dispatchLater(T message, int delayValue, TimeUnit delayUnits) { long dispatchTime = timeProvider.getCurrentTime() + delayUnits.toMillis(delayValue); lock.lock(); try { if (stopping) { throw new IllegalStateException("This dispatch has been stopped."); } queue.add(new DelayedMessage(dispatchTime, message)); condition.signalAll(); } finally { lock.unlock(); } }
public void reset() { start = timeProvider.getCurrentTime(); }
public long getTimeInMs() { return timeProvider.getCurrentTime() - start; }