所以我有 2 個演員,ClientIn和Maker.
我需要在 ClientIn 中有一個 Maker 的參考,這樣我就可以將一些收到的訊息轉發ClientIn到Maker.
我添加Maker到receptionist, 但由于clientIn是第一次創建的,maker第二次我不確定如何查詢來獲取 actorRef。
實際上,我只是對現在如何查詢接待員感到困惑,以便 clientIn 將獲得對 Maker 演員的參考。
這是我所擁有的:
// 守護者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")
....
}
ClientIn.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
}
}
}
}
Maker.scala:
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
}
}
}
}
uj5u.com熱心網友回復:
您可以向接待員發送Receptionist.Subscribe(Maker.makerKey)訊息,如 Akka 關于Actor 發現的檔案中所述:
context.system.receptionist ! Receptionist.Subscribe(Maker.makerKey, listingResponseAdapter)
參考檔案:
它將向訂閱者發送串列訊息,首先是訂閱時的條目集,然后是每當鍵的條目發生更改時。
這允許它接收制造商ActorRef,即使它在生成ClientIn演員后向接待員注冊也是如此。但是,原則上ClientIn可能會在這種情況發生之前收到其他訊息Command。它將需要處理這些情況,可能通過隱藏或洗掉命令直到Maker已注冊。
Maker在此特定示例中,通過生成第一個并將其傳遞ActorRef給來避免此要求會更簡單ClientIn.apply:
val maker = context.spawn(Maker(), "mmaker")
val clientIn = context.spawn(ClientIn(maker), "ClientIn")
然后,在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回傳的 from也能夠立即接收訊息。spawn
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/414244.html
標籤:
