1
votes

I'm trying to implement this pattern in play:

class MyController extends Controller {
    def getStuff(actorPath: String) = {
        implicit request => Async {
            val myFutureStuff = system.actorSelection(actorPath) ? FindStuff()

            // Handle the result
        }
    }
}

But apparently you can't "ask" an ActorSelection, only an ActorRef.

So you have to get an ActorRef from the ActorSelection, which means sending an "Identify" message to the ActorSelection and getting the response back. I can't find a simple way to do this from within the controller, though, as the actor has no way of sending the response of "Identify" back to it.

From examples I've seen where people communicate with actors through the controller, they use ActorRef directly. In my application, I don't want to be hanging on to these ActorRefs. Instead, I fire them up and let them go until I need to communicate with them. When that happens, I want to be able to get a handle to them via their path and ask some information of them.

Is there a better way to do what I want while still using an actor back-end/play front-end approach?

Many thanks

1

1 Answers

0
votes

I'm not sure if that is true, you should be able to use the ask pattern on an ActorSelection. So this should ideally work, the only problem with actorSelection is that since it only looks up the actor by its path it cannot guarantee that you are referring to the same actor in subsequent calls.

However, in cases when you want to receive a reply in a non-actor instance, one of the ways to do it use a future.

  implicit val timeout = Timeout(5 seconds)
  val future = system.actorSelection(remotePath) ? Identify(remotePath)
  val result = Await.result(future, timeout.duration)
  val actorRef = result match {
    case ActorIdentity(path, Some(actor)) => actor
  }