Skip to content

Instantly share code, notes, and snippets.

@sourcedelica
Last active December 16, 2015 18:21
Show Gist options
  • Save sourcedelica/5477344 to your computer and use it in GitHub Desktop.
Save sourcedelica/5477344 to your computer and use it in GitHub Desktop.
import akka.actor.{DeadLetter, ActorSystem, Props, Actor}
class Actor1 extends Actor{
override def preStart() {
context.actorOf(Props[Actor2], "actor2")
context.system.eventStream.subscribe(self, classOf[DeadLetter])
}
def receive = {
case x =>
println(x)
val as2 = context.actorSelection("/user/actor2")
as2 ! "context.actorselection top" // This never gets received
val s2 = context.system.actorSelection("/user/actor2")
s2 ! "system.actorSelection top"
val c2 = context.actorSelection("/actor2")
c2 ! "context.actorSelection child"
val af2 = context.actorFor("/user/actor2")
af2 ! "context.actorFor top"
}
}
class Actor2 extends Actor{
override def preStart() { println("my path is: " + context.self.path) }
def receive = {
case x => println(x)
}
}
object ActorSelections {
def main(args: Array[String]) {
val sys = ActorSystem("test")
implicit val ec = sys.dispatcher
//Starting an Actor1 from system which in turn starts an Actor2
val a1 = sys.actorOf(Props[Actor1], "actor1")
//Starting an Actor2 from system
val a2 = sys.actorOf(Props[Actor2], "actor2")
Thread.sleep(1000)
a1 ! "go"
Thread.sleep(1000)
sys.shutdown()
}
}
// Output is:
// my path is: akka://test/user/actor2
// my path is: akka://test/user/actor1/actor2
// go
// system.actorSelection top
// context.actorFor top
// context.actorSelection child
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment