How to get the actor system reference from inside the actor

Daniel Cukier picture Daniel Cukier · Aug 10, 2014 · Viewed 8k times · Source

I have an akka actor that send messages to itself:

  def receive = {
    while (...) {
       self ! "some message"
    }
  }

I want to use a Throttler to control the flow of messages that this actor sends to itself.

  val throttler = system.actorOf(Props(new TimerBasedThrottler(15 msgsPer (1.minute))))
  throttler ! SetTarget(Some(self))

and then change the while loop to send messages to throttler:

    while (...) {
       throttler ! "some message"
    }

The problem is that I don't know how to access the "system" from inside the actor, to create the throttler. How to do this? Is there any better approach?

Answer

Charlie S. picture Charlie S. · Jan 31, 2017

Inside actor, use context.system to access ActorSystem.