How to get an ActorRef from an ActorSelection

5,018 views
Skip to first unread message

Troy Payne

unread,
Jun 21, 2014, 10:50:07 AM6/21/14
to akka...@googlegroups.com
Hello,

I'm trying to get an ActorRef from an ActorSelection and my knowledge of Futures is a bit limited.

This is my embarrassing attempt:

        val webSocketWorker = context.actorSelection("user/websocket-worker")
        webSocketWorker.resolveOne {
            actorRef: ActorRef =>
            sender ! Http.Register(actorRef)
        }

Thanks in advance

Владимир Морозов

unread,
Jun 21, 2014, 12:44:32 PM6/21/14
to akka...@googlegroups.com
Hi,

you can try this code:
val actorRef = Await.result(system.actorSelection(actorName).resolveOne()(resolveTimeout), awaitTimeout)

best regards, Vladimir

суббота, 21 июня 2014 г., 18:50:07 UTC+4 пользователь Troy Payne написал:

Troy Payne

unread,
Jun 21, 2014, 9:59:52 PM6/21/14
to akka...@googlegroups.com
Hi,

This worked, sort of. It compiles but I'm getting that the actor isn't found. So my guess is that I need to use another method other than resolveOne?

Is there a way to list all existing actors within an actorsystem, from an actors context?

Here's my code
val actorRef = Await.result(context.actorSelection("user/websocket-worker").resolveOne(), timeout.duration)

[ERROR] [06/22/2014 01:45:56.036] [c0smo-akka.actor.default-dispatcher-5] [akka://c0smo/user/router] Actor not found for: 

ActorSelection[Anchor(akka://c0smo/user/router#-1336484262), Path(/user/user-service)]



--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/Mx8wnvu7cL4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Владимир Морозов

unread,
Jun 22, 2014, 5:48:22 AM6/22/14
to akka...@googlegroups.com
Hi!

a wrote to you complete worked example:

import akka.actor.{Props, ActorSystem, Actor}
import scala.concurrent.Await
import akka.util.Timeout
import scala.concurrent.duration._

object SomeActor {
  case object Ping
}

class SomeActor extends Actor {
  import SomeActor._

  def receive = {
    case Ping =>
      println("Pong")
  }
}

object Example extends App {
  val system: ActorSystem = ActorSystem("test-system")

  system.actorOf(Props[SomeActor], "test-actor")

  implicit val resolveTimeout = Timeout(5 seconds)

  val actorRef = Await.result(system.actorSelection("user/test-actor").resolveOne(), resolveTimeout.duration)

  actorRef ! SomeActor.Ping
}

воскресенье, 22 июня 2014 г., 5:59:52 UTC+4 пользователь Troy Payne написал:
Reply all
Reply to author
Forward
0 new messages