有没有一种标准的方法来做actorFor或其他actorOf? [英] Is there a standard way to do `actorFor orElse actorOf`?

查看:142
本文介绍了有没有一种标准的方法来做actorFor或其他actorOf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个可能已经创建的 ActorRef 。是否有一种标准方法来调用 context.actorFor ,并且仅当它没有返回实时 ActorRef 时,才调用 context.actorOf ?反之亦然(例如,调用 context.actorOf ,并且,只有在该角色已经存在的情况下,才调用 context.actorFor )。

I would like to get an ActorRef that may already have been created. Is there a standard way to call context.actorFor and, only if it didn't return a live ActorRef, call context.actorOf? Vice versa is also fine (ie call context.actorOf and, only if the actor already exists, call context.actorFor).

推荐答案

首先,get-or-create只能在只有一个实体执行的情况下起作用(否则,永远不会确定它是如何创建的)。

First off: get-or-create can only work if there is exactly one entity which does it (otherwise you will never be sure how it was created when you find it). This means that the parent of the actor-to-be is the place to put this code.

在一个演员中,这很简单:

Within an actor it is quite straight-forward:

val child =
  context.child(name) match {
    case None    => context.actorOf(Props(...), name)
    case Some(c) => c
  }

请不要使用 actorFor ,则在充分理由。在这种情况下, context.child()可以更有效地完成您想要的操作。

Please refrain from using actorFor, it is deprecated in Akka 2.2 for good reason. In this case context.child() does what you want more efficiently.

这篇关于有没有一种标准的方法来做actorFor或其他actorOf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆