private Reply syncSend(Message msg, Duration timeout) { if (timeout != null) { msg.setTimeRemaining(timeout.toMillis()); } try { RequestMonitor monitor = new RequestMonitor(); msg.setContext(monitor); msg.pushHandler(this); // store monitor Result result = null; while (result == null || result.type() == Result.ResultType.TRANSIENT_ERROR) { result = session.send(msg); if (result != null && result.isSuccess()) { break; } Thread.sleep(100); } if (!result.isSuccess()) { throw new DocumentAccessException(result.getError().toString()); } return monitor.waitForReply(); } catch (InterruptedException e) { throw new DocumentAccessException(e); } }
@Override public void handleReply(Reply reply) { if (reply.getContext() instanceof RequestMonitor) { ((RequestMonitor)reply.getContext()).replied(reply); } else { ReplyHandler handler = reply.getCallStack().pop(reply); handler.handleReply(reply); // not there yet } }