public MessageAwaiterImpl(AgentItem agent, String canvasId, Phase phase) { this.agent = agent; this.canvasId = canvasId; this.phase = phase; agent.addMessageListener(CHANNEL, this); agent.addConnectionListener(this); agent.sendMessage(CHANNEL, new Object[] {canvasId, phase.toString()}); }
@Override public boolean await() { synchronized (this) { deadline = System.currentTimeMillis() + TIMEOUT; while (!done) { try { if (agent.isReady()) { wait(deadline - System.currentTimeMillis()); } if (!done && System.currentTimeMillis() >= deadline) { log.error("Timed out waiting for Agent: {}", agent); complete(); return false; } } catch (InterruptedException e) { e.printStackTrace(); } } return true; } }
synchronized void complete() { agent.removeMessageListener(this); agent.removeConnectionListener(this); done = true; notifyAll(); }