Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value ! is not a member of Actor

Tags:

scala

akka

actor

I've got an Actor class that manages a list of Actors. When it receives a message of a certain type, it passes it off to each of the Actors it knows about.

 var loggers : List[Logger]    

def receive = {
  ...
  // log request
  case logmessage : LogMessage => {
      // send message to each logger (if none, nothing happens)
      for (logger <- loggers)
        logger ! logmessage
  }
  ...     
}

I'm getting a compile error on logger ! logmessage : "value ! is not a member of (package).Logger". The ! makes this very difficult to google. The Logger class compiles, having its own receive method, one line including self ! PoisonPill, meaning the ! operator works there. They're in the same package.

like image 805
CPS Avatar asked Dec 20 '22 20:12

CPS


1 Answers

I'm assuming Logger extends Actor

In Akka, message sending is done between ActorRefs, not Actors. Every actor's self is an ActorRef instance. So you would want to change your loggers var to be var loggers: List[ActorRef].

Better yet, you could use an ActorSelection, which has a ! method that sends the message to all actor refs in the selection. You can find methods for creating an ActorSelection in the Actor's context.

P.S. You can use the "#" page on the Akka Scaladocs to find methods that start with symbols. On that page you can see that there is a ! defined on ScalaActorRef and ScalaActorSelection, both of which are implicit upgrades from ActorRef and ActorSelection.

like image 85
Dylan Avatar answered Jan 06 '23 19:01

Dylan