作为 actorSelection 与 actorOf 的区别

Akka actorSelection vs actorOf Difference

这两者有区别吗?当我这样做时:

context.actorSelection(actorNameString)

我得到一个 ActorSelection 引用,我可以使用 resolveOne 解析它,然后我得到一个 Future[ActorRef]。但是有了 actorOf,我会立即得到一个 ActorRef。除此之外还有其他重要的区别吗?

我希望将 ActorRef 包装在 Future 中的用例是什么?

actorOf 用于通过提供道具对象来创建新演员

actorSelection"pointer" 到角色树 中的路径。通过使用 resolveOne,您将获得该路径下已经存在的 actor 的 actorRef - 但该 actorRef 需要时间来解析,因此是 Future。

这里有更详细的解释: http://doc.akka.io/docs/akka/snapshot/general/addressing.html

An actor reference designates a single actor and the life-cycle of the reference matches that actor’s life-cycle; an actor path represents a name which may or may not be inhabited by an actor and the path itself does not have a life-cycle, it never becomes invalid. You can create an actor path without creating an actor, but you cannot create an actor reference without creating corresponding actor.

在任一过程中,生产 ActorRef 都会产生相关成本。 使用 system.actorOf 创建用户顶级 actors 成本很高,因为它必须处理错误的内核初始化,这也很昂贵。从子 actor 创建 ActorRef 非常公平,适合每个任务设计一个 actor。如果在一个应用程序中,对于每个请求,都会在没有清理的情况下创建一组新的 actor,尽管 akka actors 很便宜,但您的应用程序可能 运行 内存不足。另一个好处是 actorOf 正如你提到的那样是立竿见影的。

用抽象术语来说,actorSelectionresolveOne 会查找 actor 树并在未来生成一个 actorRef,因为这不是那么直接,尤其是在远程系统上。但它强制执行 re-usability。 Futures 抽象了解析 ActorRef 的等待时间。

这里是 ActorOf 与 ActorSelection 的简要总结;希望对您有所帮助:

https://getakka.net/articles/concepts/addressing.html

Actor references may be looked up using the ActorSystem.ActorSelection method. The selection can be used for communicating with said actor and the actor corresponding to the selection is looked up when delivering each message.

In addition to ActorSystem.actorSelection there is also ActorContext.ActorSelection, which is available inside any actor as Context.ActorSelection. This yields an actor selection much like its twin on ActorSystem, but instead of looking up the path starting from the root of the actor tree it starts out on the current actor.

Summary: ActorOf vs. ActorSelection

ActorOf only ever creates a new actor, and it creates it as a direct child of the context on which this method is invoked (which may be any actor or actor system). ActorSelection only ever looks up existing actors when messages are delivered, i.e. does not create actors, or verify existence of actors when the selection is created.