Akka演员“问”和“等待”与TimeoutException [英] Akka Actor "ask" and "Await" with TimeoutException
问题描述
我正在用Scala和Akka建模一个简单的P2P:
I'm modeling a simple P2P with Scala and Akka:
class Node() extends Peer with Actor {
var peers: List[ActorRef] = List()
def receive = {
case _register(peer: ActorRef, p: Option[Int]) => {
println("registering [" + peer + "] for [" + this + "]")
peers = peer :: peers
}
}
}
sealed case class _register(val peer: ActorRef, var p: Option[Int] = None)
然后是一个简单的网络:
and then a simple network:
class Network() extends Actor {
def this(name: String) = {
this()
val system = ActorSystem(name)
val s1 = system.actorOf(Props(new Node()), name = "s1")
val s2 = system.actorOf(Props(new Node()), name = "s2")
val c1 = system.actorOf(Props(new Node()), name = "c1")
val c2 = system.actorOf(Props(new Node()), name = "c2")
val c3 = system.actorOf(Props(new Node()), name = "c3")
val c4 = system.actorOf(Props(new Node()), name = "c4")
implicit val timeout = Timeout(5 second)
s1 ? _register(c1)
s1 ? _register(c2)
s1 ? _register(c3)
val lastRegistered = s2 ? _register(c4)
Await.ready(lastRegistered, timeout.duration)
println("initialized nodes")
}
}
我得到的输出总是 ,例如:
The output that I'm getting is always like:
registering [Actor[akka://p2p/user/c1]] for [nl.cwi.crisp.examples.p2p.scala.Node@14b5f4a]
registering [Actor[akka://p2p/user/c2]] for [nl.cwi.crisp.examples.p2p.scala.Node@14b5f4a]
registering [Actor[akka://p2p/user/c3]] for [nl.cwi.crisp.examples.p2p.scala.Node@14b5f4a]
registering [Actor[akka://p2p/user/c4]] for [nl.cwi.crisp.examples.p2p.scala.Node@13c0b53]
[ERROR] [04/10/2012 22:07:04.34] [main-akka.actor.default-dispatcher-1] [akka://main/user/p2p] error while creating actor
java.util.concurrent.TimeoutException: Futures timed out after [5000] milliseconds
at akka.dispatch.DefaultPromise.ready(Future.scala:834)
at akka.dispatch.DefaultPromise.ready(Future.scala:811)
at akka.dispatch.Await$.ready(Future.scala:64)
at nl.cwi.crisp.examples.p2p.scala.Network.<init>(Node.scala:136)
at nl.cwi.crisp.examples.p2p.scala.Main$$anonfun$11.apply(Node.scala:164)
at nl.cwi.crisp.examples.p2p.scala.Main$$anonfun$11.apply(Node.scala:164)
at akka.actor.ActorCell.newActor(ActorCell.scala:488)
at akka.actor.ActorCell.create$1(ActorCell.scala:506)
at akka.actor.ActorCell.systemInvoke(ActorCell.scala:591)
at akka.dispatch.Mailbox.processAllSystemMessages(Mailbox.scala:191)
at akka.dispatch.Mailbox.run(Mailbox.scala:160)
at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:505)
at akka.jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:259)
at akka.jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:997)
at akka.jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1495)
at akka.jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104)
我遵循了未来。将 Await.ready
与 Await.result
交换没有任何效果。日志显示上一次注册已成功。
I have followed the documentation of Futures on Akka reference documentation. Exchanging Await.ready
with Await.result
does not have any effect. The log shows that the last registration has been successful.
我该如何解决?
推荐答案
您正在等待消息将从Node actor返回,但是Node actor不会将消息发送回 sender
actorRef,因此<$ c $创建的Future [Any] c> s1? _register 将永远不会收到响应,因此Future将永远不会完成。您可以添加 sender!节点
发送响应,我不确定接收
方法中的某事某事
在这种情况下很有意义。
You are waiting for a message to be returned from the Node actor, but the Node actor does not send a message back to the sender
actorRef, so the Future[Any] created by s1 ? _register
will never receive a response, so the Future will never be complete. You could add sender ! something
from inside the Node receive
method to send a response, I'm not sure what something
makes sense in this case.
这篇关于Akka演员“问”和“等待”与TimeoutException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!