原文链接 http://ibruce.info/2014/05/20/hello-akka/
package test.akka.hh import akka.actor.Actor import akka.actor.ActorSystem import scala.concurrent.Await import akka.actor.ActorRef import akka.actor.Props class Heng(name: String, ha: ActorRef) extends Actor { val selfStr = self.toString() implicit val to = akka.util.Timeout.apply(3, java.util.concurrent.TimeUnit.SECONDS) var haAR: ActorRef = null val haActorRefFuture = context.actorSelection("akka://HengHaSystem/user/ha").resolveOne val result = Await.result(haActorRefFuture, to.duration) def receive: Actor.Receive = { case "start" => ha ! "heng" case "ha" => val from = result.toString() println(result) println(s"[$selfStr] received 哈 from[$from]") ha ! "heng" case _ => println("heng what?") } } class Ha(name: String) extends Actor { val selfStr = self.toString() def receive = { case "heng" => val from = sender.toString() println(s"[$selfStr] received 哼 from[$from]") sender ! "ha" case _ => println("ha what?") } } object HengHa { def main(args: Array[String]): Unit = { test } def test = { val system = ActorSystem("HengHaSystem") val ha = system.actorOf(Props(classOf[Ha], "HA"), name = "ha") val heng = system.actorOf(Props(new Heng("HENG", ha)), name = "heng") heng ! "start" } }