Alex Stanovsky
Alex Stanovsky

Reputation: 1376

Akka Remote actor selection with configuration

Is there a way for setting configuration for remote actor selection simliar to the remote actor creation as specified in Akka docs:

akka {
  actor {
    deployment {
      /sampleActor {
        remote = "akka.tcp://[email protected]:2553"
      }
    }
  }
}

I prefer not to define custom variable for that.

system.actorSelection("sampleActor")

Upvotes: 1

Views: 928

Answers (1)

mattinbits
mattinbits

Reputation: 10428

There are only two forms of the actor selection method, from the docs:

def actorSelection(path: ActorPath): ActorSelection

Construct an akka.actor.ActorSelection from the given path, which is parsed for wildcards (these are replaced by regular expressions internally). No attempt is made to verify the existence of any part of the supplied path, it is recommended to send a message and gather the replies in order to resolve the matching set of actors.

def actorSelection(path: String): ActorSelection

Construct an akka.actor.ActorSelection from the given path, which is parsed for wildcards (these are replaced by regular expressions internally). No attempt is made to verify the existence of any part of the supplied path, it is recommended to send a message and gather the replies in order to resolve the matching set of actors.

And an ActorPath is just created from a string anyway:

def fromString(s: String): ActorPath

Parse string as actor path; throws java.net.MalformedURLException if unable to do so.

So there isn't a direct way to do actor selection just by setting a particular value in config. However it is quite easy to pull a value from config and use it for actor selection. Given the config:

akka {
  actor {
    selections: {
      sampleActor: {
        path: "akka.tcp://[email protected]:2553/user/sampleActor"
      }
    }
  }
}

You could use:

val sampleActorSelection = 
  system.actorSelection(
    system.settings.config.getString("akka.actor.selections.sampleActor.path"))

If this was a method you found yourself using frequently, you could use an implicit class to add a helper method to system:

implicit class ActorSystemExtension(system: ActorSystem) {

  def actorSelectionFromConfig(actorName: String): ActorSelection {
    system.actorSelection(
        system.settings.config.getString(s"akka.actor.selections.${actorName}.path"))
  }
}

Upvotes: 1

Related Questions