代码之家  ›  专栏  ›  技术社区  ›  Blankman

我怎样才能从接待员那里获得ActorRef,这样我的演员就可以向那个演员发送消息

  •  0
  • Blankman  · 技术社区  · 4 年前

    我有两个演员, ClientIn 和 Maker

    我需要有一个制造商在客户端内的参考,所以我可以转发一些信息,其中包括 客户端 接收到 .

    我补充道 receptionist ,但自 clientIn 首先创建,并且 maker 第二,我不知道如何查询以获取actorRef。

    //guardian actor构造函数:

    Behaviors.setup[Any] { context =>
        implicit val ec: ExecutionContext = context.executionContext
    
        val system = context.system
    
        val clientIn = context.spawn(ClientIn(), "ClientIn")
    
        val maker = context.spawn(Maker(clientOut), "mmaker")
    
        ....
    
    }    
    

    客户端。scala:

    object ClientIn {
      sealed trait ClientInCommand
      case class Watch(symbol: String) extends ClientInCommand
      case class Command(value: String) extends ClientInCommand
      private case class ListingResponse(listing: Receptionist.Listing)
          extends ClientInCommand
    
      def apply(): Behavior[ClientInCommand] = {
    
        Behaviors.setup { context =>
          context.log.info(s"ClientIn starting...${context.self}")
    
          val listingResponseAdapter =
            context.messageAdapter[Receptionist.Listing](ListingResponse.apply)
    
          var maker: Option[ActorRef[Maker.MMCommand]] = None
    
          Behaviors.receiveMessage[ClientInCommand] {
            case Watch(symbol) =>
              context.log.info(s"ClientIn watch message received..$symbol")
              Behaviors.same
            case Command(value) =>
              context.log.info(s"ClientInCommand.Command with value:$value")
    
              // TODO: Maker ! RecieveCommand(value)
              Behaviors.same
            case ListingResponse(Maker.makerKey.Listing(listings)) =>
              maker = Some(listings.head)
              Behaviors.same
    
          }
        }
    
      }
    }      
    

    object Maker {
      val makerKey = ServiceKey[MMCommand]("makerKey")
    
      sealed trait MMCommand
      case object Startup extends MMCommand
      case object Shutdown extends MMCommand
    
      def apply(): Behavior[MMCommand] = {
    
        Behaviors.setup { context =>
    
          context.system.receptionist ! Receptionist.Register(
            makerKey,
            context.self
          )
    
          Behaviors.receiveMessage[MMCommand] {
            case Startup =>
              context.log.info("Maker startup message received..")
              Behaviors.same
            case Shutdown =>
              context.log.info("Maker shutdown message received..")
              Behaviors.same
          }
    
        }
    
      }
    }
    
    0 回复  |  直到 4 年前
        1
  •  4
  •   Tim Moore    4 年前

    你可以发一封信 Receptionist.Subscribe(Maker.makerKey) 发送给接待员的信息,如Akka文档中所述 Actor discovery

    context.system.receptionist ! Receptionist.Subscribe(Maker.makerKey, listingResponseAdapter)
    

    引用文件:

    这允许它接收制造商 ActorRef ClientIn 可能会收到其他消息,例如 Command 在这之前。它将需要处理这些案件,也许需要 stashing 或删除命令,直到 Maker

    首先,通过 ActorRef公司 到 ClientIn.apply

    val maker = context.spawn(Maker(), "mmaker")
    val clientIn = context.spawn(ClientIn(maker), "ClientIn")
    

    然后,在 客户端

    def apply(maker: ActorRef[Maker.MMCommand]): Behavior[ClientInCommand] = {
      Behaviors.setup { context =>
        context.log.info(s"ClientIn starting...${context.self}")
        Behaviors.receiveMessage[ClientInCommand] {
          case Watch(symbol) =>
            context.log.info(s"ClientIn watch message received..$symbol")
            Behaviors.same
          case Command(value) =>
            context.log.info(s"ClientInCommand.Command with value:$value")
            maker ! Maker.ReceiveCommand(value)
            Behaviors.same
        }
      }
    }
    

    请注意 ActorRef公司 spawn 即使参与者尚未完全初始化,也能够立即接收消息。

    推荐文章