经典演员系统向 akka 类型的演员发送消息
Classic actor system sending messages to a akka typed actor
在我的应用程序中,我同时拥有经典的 actor 系统和 typedGuardian(使用 akka typed)。
implicit val system = akka.actor.ActorSystem("my-classic-actor-system")
implicit val typedGuardian: ActorRef[Any] =
system.spawn(HttpWebsocketServer(), "HttpServer")
Poly.startProcessor(?????) // pass in WebSocketIn here which is a typed actor
我的 akka 类型设置从下面的演员开始,我需要以某种方式将客户端 webSockedIn akka 类型的演员作为参数传递给我的 Poly.startProcessor 经典演员。
我需要这样做,因为我需要将消息从其中发送到 webSocketIn,这样我就可以在我的 HttpWebsocketServer 中处理这些消息。
HttpWebsocketServer:
def apply(): Behavior[Any] = {
Behaviors.setup[Any] { context =>
implicit val ec: ExecutionContext = context.executionContext
val system = context.system
val webSockedIn = context.spawn(websock.WebSocketIn(), "WebSocketIn")
我需要以某种方式将 webSockedIn 传递给 Poly.startProcessor:
object Poly {
def startProcessor()(implicit system: akka.actor.ClassicActorSystemProvider): Unit = {
经典演员系统是否有可能获得对 akka 类型演员的引用并发送消息呢?
如果是这样,我应该在哪里生成我的 websocketClientIn 以便这成为可能?
假设您控制 Poly
中的实现,您可以让 startProcessor
通过 system.actorSelection("/user/HttpServer")
.[=22] 解析 typedGuardian
/HttpWebsocketServer
参与者=]
执行此操作后,您可以让 HttpServer
回答 websockedIn
的查询消息(例如通过询问模式)。
所以像
// in HttpWebsocketServer
case class ResolveWebsockedIn(replyTo: ActorRef[ActorRef[WebsocketIn.Command]]) extends Command
case ResolveWebsockedIn(replyTo) =>
replyTo ! webSockedIn
Behaviors.same
// in Poly.startProcessor
import scala.concurrent.duration._
val resolvedFut = system.actorSelection("/user/HttpServer").resolveOne
val websockedInFut = resolvedFut.flatMap { classicRef =>
import akka.actor.typed.scaladsl.adapter.ClassicActorRefOps
import akka.actor.typed.scaladsl.adapter.ClassicActorSystemOps
import akka.actor.typed.scaladsl.AskPattern._
classicRef.toTyped[HttpWebsocketServer.ResolveWebsockedIn].ask(HttpWebsocketServer.ResolveWebsockedIn(_))(10.seconds, system.toTyped.scheduler)
}
websockedInFut
未来由 HttpWebsocketServer
产生的 webSockedIn
完成。
在我的应用程序中,我同时拥有经典的 actor 系统和 typedGuardian(使用 akka typed)。
implicit val system = akka.actor.ActorSystem("my-classic-actor-system")
implicit val typedGuardian: ActorRef[Any] =
system.spawn(HttpWebsocketServer(), "HttpServer")
Poly.startProcessor(?????) // pass in WebSocketIn here which is a typed actor
我的 akka 类型设置从下面的演员开始,我需要以某种方式将客户端 webSockedIn akka 类型的演员作为参数传递给我的 Poly.startProcessor 经典演员。 我需要这样做,因为我需要将消息从其中发送到 webSocketIn,这样我就可以在我的 HttpWebsocketServer 中处理这些消息。
HttpWebsocketServer:
def apply(): Behavior[Any] = {
Behaviors.setup[Any] { context =>
implicit val ec: ExecutionContext = context.executionContext
val system = context.system
val webSockedIn = context.spawn(websock.WebSocketIn(), "WebSocketIn")
我需要以某种方式将 webSockedIn 传递给 Poly.startProcessor:
object Poly {
def startProcessor()(implicit system: akka.actor.ClassicActorSystemProvider): Unit = {
经典演员系统是否有可能获得对 akka 类型演员的引用并发送消息呢?
如果是这样,我应该在哪里生成我的 websocketClientIn 以便这成为可能?
假设您控制 Poly
中的实现,您可以让 startProcessor
通过 system.actorSelection("/user/HttpServer")
.[=22] 解析 typedGuardian
/HttpWebsocketServer
参与者=]
执行此操作后,您可以让 HttpServer
回答 websockedIn
的查询消息(例如通过询问模式)。
所以像
// in HttpWebsocketServer
case class ResolveWebsockedIn(replyTo: ActorRef[ActorRef[WebsocketIn.Command]]) extends Command
case ResolveWebsockedIn(replyTo) =>
replyTo ! webSockedIn
Behaviors.same
// in Poly.startProcessor
import scala.concurrent.duration._
val resolvedFut = system.actorSelection("/user/HttpServer").resolveOne
val websockedInFut = resolvedFut.flatMap { classicRef =>
import akka.actor.typed.scaladsl.adapter.ClassicActorRefOps
import akka.actor.typed.scaladsl.adapter.ClassicActorSystemOps
import akka.actor.typed.scaladsl.AskPattern._
classicRef.toTyped[HttpWebsocketServer.ResolveWebsockedIn].ask(HttpWebsocketServer.ResolveWebsockedIn(_))(10.seconds, system.toTyped.scheduler)
}
websockedInFut
未来由 HttpWebsocketServer
产生的 webSockedIn
完成。