call methods on akka actors in scala

前端 未结 3 2175
甜味超标
甜味超标 2021-02-20 15:10

I have a an actor defined as so:

class nodeActor(ID: String) extends Actor

which contains a method, which is used to set up the actor before it

3条回答
  •  盖世英雄少女心
    2021-02-20 15:37

    You're not supposed to call an actor's methods directly from another class. It breaks the whole design of the system, which is

    • to encapsulate the actor's specific implementation by communicating only with the ActorRef obtained with the call to actorOf or actorFor
    • to limit communication between actors to message passing, using the available (!, ?) methods

    If you need to create a reference in ActorA to another ActorB you can:

    • Create the ref to ActorB in the ActorA's initialization code as shown in http://doc.akka.io/docs/akka/2.0.3/scala/actors.html
    • Send the ActorB's reference to the ActorA as a specific message. Then ActorA can store the reference within receive implementation

    If you need to call a method to satisfy an Interface/Trait constraint, have a look at Typed Actors

提交回复
热议问题