Akka ask() 超时不起作用
Akka ask() timeout does not work
我正在通过执行以下代码对不同的代理进行两次调用。
Timeout t = new Timeout(100, TimeUnit.MILLISECONDS);
Future<Object> eventFuture = ask(this.customerEventActor, new GetCustomerEventActionsMessage(customerCookie), t);
Future<Object> infoFuture = ask(this.customerPersonalInfoActor, new GetCustomerPersonalInfoMessage(customerCookie), t);
在我的 actor 中,我将线程暂停 1000 毫秒并返回结果。我希望它会失败超时错误。但后来我看到结果状态是成功的并且有有效的回应。 EVENTS! Time taken: 4931
所以执行了4931ms,并没有超时。为什么?
public class CustomerEventActor extends UntypedActor {
@Override
public void onReceive(Object o) throws Exception {
long now = System.currentTimeMillis();
Thread.sleep(1000);
CustomerEventResponseMessage msg = new CustomerEventResponseMessage();
msg.events = "EVENTS! Time taken: " + (System.currentTimeMillis() - now);
getSender().tell(msg, getSelf());
getContext().stop(getSelf());
}
}
当 future 失败时,它不会抛出异常。
未来有两种可能的完成方式:
1) Success(result) - 然后你可以得到未来的结果
2) Failure(failure) - 你可以获得通常是异常的失败参数(在你的情况下是 AskTimeoutException)
你从未真正看过已完成的未来,所以你无法知道它是否成功。
我正在通过执行以下代码对不同的代理进行两次调用。
Timeout t = new Timeout(100, TimeUnit.MILLISECONDS);
Future<Object> eventFuture = ask(this.customerEventActor, new GetCustomerEventActionsMessage(customerCookie), t);
Future<Object> infoFuture = ask(this.customerPersonalInfoActor, new GetCustomerPersonalInfoMessage(customerCookie), t);
在我的 actor 中,我将线程暂停 1000 毫秒并返回结果。我希望它会失败超时错误。但后来我看到结果状态是成功的并且有有效的回应。 EVENTS! Time taken: 4931
所以执行了4931ms,并没有超时。为什么?
public class CustomerEventActor extends UntypedActor {
@Override
public void onReceive(Object o) throws Exception {
long now = System.currentTimeMillis();
Thread.sleep(1000);
CustomerEventResponseMessage msg = new CustomerEventResponseMessage();
msg.events = "EVENTS! Time taken: " + (System.currentTimeMillis() - now);
getSender().tell(msg, getSelf());
getContext().stop(getSelf());
}
}
当 future 失败时,它不会抛出异常。
未来有两种可能的完成方式:
1) Success(result) - 然后你可以得到未来的结果
2) Failure(failure) - 你可以获得通常是异常的失败参数(在你的情况下是 AskTimeoutException)
你从未真正看过已完成的未来,所以你无法知道它是否成功。