Blankman
Blankman

Reputation: 267000

Classic actor system sending messages to a akka typed actor

In my application I have both the classic actor system and a typedGuardian (using 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

My akka typed setup starts off with the below actor, I need to somehow pass the client webSockedIn akka typed actor as an argument to my Poly.startProcessor classic actor. I need to to this because I need to send messages from inside of this to webSocketIn so I can use handle those messages inside of my 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")

I need to somehow pass in the webSockedIn to Poly.startProcessor:

object Poly {
  def startProcessor()(implicit system: akka.actor.ClassicActorSystemProvider): Unit = {  

      

Is it possible to a classic actor system to get a reference to a akka typed actor and send messages too it?

If so, where should I spawn my websocketClientIn such that this would be possible?

Upvotes: 0

Views: 270

Answers (1)

Levi Ramsey
Levi Ramsey

Reputation: 20551

Assuming that you control the implementation in Poly, you can have startProcessor resolve the typedGuardian/HttpWebsocketServer actor via system.actorSelection("/user/HttpServer").

Doing this, you can then have HttpServer answer a query message for websockedIn (e.g. via the ask pattern).

So something like

// 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)
}

The websockedInFut future is thus completed with the webSockedIn spawned by the HttpWebsocketServer.

Upvotes: 1

Related Questions