简体   繁体   中英

Scala akka typed: how to get ActorRef to Actor from it's instance and send message itself?

I want to send message from Actor instance (case class/class from what it's behaviour created) to it's Actor.

I gain it by saving instance, and then save ActorRef in it:

val (instance, behaviour) = MyActorInstance(Nothing)
val actor = ActorSystem(instance, "SomeName123")
//save it here
instance.setMyActor(actor)

object MyActorInstance {
  def apply(ctx: ActorContext[Commands]): (MyActorInstance,Behavior[Commands]) = {
    val actorInstance = new MyActorInstance(ctx)
    val behaviour: Behavior[Commands] =
      Behaviors.setup { context =>
        {
          Behaviors.receiveMessage { msg =>
            actorInstance.onMessage(msg)
          }
        }
      }
    (actorInstance,behaviour)
  }
}

class MyActorInstance(context: ActorContext[Commands]) extends AbstractBehavior[Commands](context) {

  protected var myActorRef: ActorRef[Commands] = null

  def setMyActor(actorRef: ActorRef[Commands]): Unit = {
    myActorRef = actorRef
  }

  override def onMessage(msg: Commands): Behavior[Commands] = {
    msg match {
      case SendMyself(msg) =>
        myActorRef ! IAmDone(msg)
        Behaviors.same
      case IAmDone(msg) =>
        println(s"Send $msg to myself!")
        Behaviors.same
    }
  }
}

Here i save ActorRef to Actor for it's instance in var myActorRef . Then i use that myActorRef to send message from Actor's instance to itself by SendMyself message.

But for that, as you see, i am using variables, which is not good: to save ActorRef it's need to rewrite field myActorRef of instance of MyActorInstance class from null to ActorRef - it is possible only with var iables.

If I try to use val and create immutable class by rewriting its instance for new, and then swap it from old to new, my Actor actor still linked to old instance where myActorRef == null .

Now I found one way: just using var instead of val or immutable class.

But I want to use val or nothing. For that I need to get ActorRef from it's instance, but how?

There is no reason for such complex dance, just use ctx.self . And please read at least the most basic documentation before asking, it would even have saved you time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM