@Override public void onReceive(final Object message) throws Exception { if (message instanceof AskParam) { AskParam askParam = (AskParam) message; timeout = askParam.timeout; caller = sender(); targetActor = context().actorOf(askParam.props); context().watch(targetActor); targetActor.forward(askParam.message, context()); final Scheduler scheduler = context().system().scheduler(); timeoutMessage = scheduler.scheduleOnce( askParam.timeout.duration(), self(), new AskTimeout(), context().dispatcher(), null); } else if (message instanceof Terminated) { sendFailureToCaller(new ActorKilledException("Target actor terminated.")); timeoutMessage.cancel(); context().stop(self()); } else if (message instanceof AskTimeout) { sendFailureToCaller( new TimeoutException("Target actor timed out after " + timeout.toString())); context().stop(self()); } else { unhandled(message); } }