scalaakka-actor

Is there a way to use arbitary implemented methods of extended Actor classes in an akka app?


In my situation, I have an Actor class (say Greeter) with a sendSecureMessage method that I want to use in the app. The problem is after the creation of a greeter actor, ActorSystem provides me with just an ActorRef reference to Greeter and I can't use sendSecureMessage with that reference. is there a way in which I can call this function?

def sendSecureMessage(sender: ActorRef,receiver: ActorRef, message: Any, automata: Automata):

that's the method in class Greeter(message: String, printerActor: ActorRef) extends Actor

and this is how I create the actors:

  val firstActor: ActorRef =
       system.actorOf(Greeter.props("first",printer).withDispatcher("custom-dispatcher"),"firstActor")

Solution

  • There are two options here.

    If sendSecureMessage is a "static" method that does not use actor state, then put it in the companion object for the actor and call it directly from there.

    If sendSecureMessage does use actor state then the only safe way to call it is to send a message with the parameters to the actor, have the actor call the method, then send the results back as a reply. If you allow code to call methods on the actor from a different thread then all the synchronisation guarantees go out of the window.