【问题标题】:Convert ActorSelection to ActorRef将 ActorSelection 转换为 ActorRef
【发布时间】:2016-02-06 05:47:40
【问题描述】:

我有一些分布式演员,我需要这些演员的 ActorRef。所以我尝试了这一行(它在 MemberUp 代码中)。

implicit val resolveTimeout = Timeout(5 seconds)
var act=Await.result(context.system.actorSelection(RootActorPath(member.address)/"user"/"myactor2").resolveOne(), resolveTimeout.duration)

但我总是遇到以下异常:

[错误] [11/03/2015 16:01:58.530] [ClusterSystem-akka.actor.default-dispatcher-5] [akka://ClusterSystem/user/myactor1] 找不到演员: ActorSelection[锚(akka://ClusterSystem/),路径(/user/myactor2)] akka.actor.ActorNotFound:找不到演员: ActorSelection[Anchor(akka://ClusterSystem/), Path(/user/myactor2)]

我尝试将超时时间增加到 15 或 20 秒,但仍然遇到同样的错误。问题是,如果我保留 ActorSelection (

act=context.system.actorSelection(RootActorPath(member.address)/"user"/"myactor2");

),这条线工作正常,我可以向其他演员发送消息。为什么?

【问题讨论】:

    标签: scala akka cluster-computing actor


    【解决方案1】:

    为什么不简单

    val actor = context.system.actorSelection(RootActorPath(member.address)/"user"/"myactor2")
    actor ! message // you can directly send message to it!
    

    如果您确定该路径中存在演员,则可以发送这样的消息。正如官方文档所说:

    actorSelection 仅在传递消息时查找现有参与者,即不创建参与者,或在创建选择时验证参与者的存在。

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      • 2014-07-20
      相关资源
      最近更新 更多