Akka Actor “ask” and “Await” with TimeoutException

依然范特西╮ 提交于 2019-12-04 06:49:20
stew

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.

stew's got it right, but you've got some worrisome code in your network actor:

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")

Why are you creating a new ActorSystem, and why are you creating top-level actors inside that actor system?

If you need access to the actor's system, you simple call:

context.system

And you should avoid creating top-level actors "just because", for the same reason that you shouldn't clutter the root of your file system by placing all your files there. To create child-actors to Network, just do:

context.actorOf(...)

Right now you'll have a problem as soon as you create more than one Network-actor in the same system, as it will attempt to create top-level actors of the same name.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!