Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Passing zio actor references between Scala objects #482

@Raff97

Description

@Raff97

Hi,
I am wondering if it is possible to instantiate inside an object in Scala (called for example FirstObject) a zio actor, and then be able to pass the reference of this actor into another Scala object (called for example SecondObject), and to call in SecondObject the ask method (?) on the actor reference , so as to send messages to the actor.

I have this doubt because I have done some trials, but only if I send messages to the actor via the ask method in the object in which the actor is created (FirstObject), do I get a response from the actor, otherwise if I call the ask method in SecondObject, the application remains blocked waiting for the response of the actor (without ever receiving it).

I tried in FirstObject to save the actor reference in the variable reference


  val myAppLogic = for {
    system <- ActorSystem("mySystem")
    MyActorReference <- system.make("MyActor ", Supervisor.none, (), stateful)
  } yield (reference = MyActorReference )

and then pass this variable inside SecondObject, so as to insert it in the zio effect:

 val myAppLogic = for {
   response<- reference ? MyCommand(SomeCommand)
   _   <- printLine("The response is:  ",response)
  } yield ()

Could someone help me figure out what is wrong with my code, or in general if it is possible to implement this behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions