1. 程式人生 > >Akka 快速入門

Akka 快速入門

Akka的優點太多,高效能、高可靠、高併發、分散式、可容錯、可擴充套件、事件驅動,不一一敘述。不同版本的API差異很大,本文程式碼執行在 Scala 2.10.3 和 Akka 2.3.2 之上。

<dependency>
  <groupId>com.typesafe.akka</groupId>
  <artifactId>akka-actor_2.10</artifactId>
  <version>2.3.2</version>
</dependency>
<dependency>
  <groupId
>
org.scala-lang</groupId>
<artifactId>scala-library</artifactId> <version>2.10.3</version> </dependency>

定義

定義Actor很簡單,繼承 akka.actor.Actor ,實現receive方法即可。

class Hello extends Actor {
  def receive = {
    case msg: String => println("hello " + msg)
    case
_ => println("unexpected message.")
} }

啟動

建立Actor例項需要通過 ActorSystem 。

val system = ActorSystem("HelloSystem")
val hello = system.actorOf(Props[Hello], name = "hello")
val hello1 = system.actorOf(Props[Hello])
val hello2 = system.actorOf(Props(new Hello()))

如果要在 Actor 中繼續建立子 Actor,需要使用內建的 ActorContext 物件。

context.actorOf(Props[children], name = "children")

如果要建立遠端 Actor,需要通過 actorSelection 方法,原 actorFor 方法不再使用。

context.actorSelection("akka.tcp://[email protected]:5150/user/RemoteActor")

發訊息

巨簡單,就是一個!,可以傳送任意型別的訊息,此訊息是非同步的。

hello ! "bruce"
hello ! 10086

同步訊息的傳送需要使用 Future 物件。

implicit val timeout = Timeout(5 seconds)
val future = hello ? "sha"
val result = Await.result(future, timeout.duration).asInstanceOf[String]

停止

有兩種方式停止一個Actor。

一種是通過內部 ActorContext.stop() 方法,該方法會將 children actor 逐層殺掉後,再自刎。

def receive = {
    case "stop" => context.stop(self)
    ...
  }

另一種是外部喂毒藥,通過 ActorRef.tell() 方法實現。後一個引數是向誰reply,這裡顯然不需要,傳空。

hello.tell(PoisonPill.getInstance, ActorRef.noSender);

哼哈示例

哼哈二將本是兩位佛寺的門神俗稱,是執金剛神的一種。明代小說《封神演義》作者陳仲琳據此附會兩員神將,形象威武凶猛。一名鄭倫,能鼻哼白氣制敵;一名陳奇,能口哈黃氣擒將。

object HengHa extends App {
  val system = ActorSystem("HengHaSystem")
  val ha = system.actorOf(Props[Ha], name = "ha")
  val heng = system.actorOf(Props(new Heng(ha)), name = "heng")

  heng ! "start"
}
class Heng(ha: ActorRef) extends Actor {
  def receive = {
    case "start" => ha ! "heng"
    case "ha" => 
      println("哈")
      ha ! "heng"
    case _ => println("heng what?")
  }
}
class Ha extends Actor {
  def receive = {
    case "heng" => 
      println("哼")
      sender ! "ha"
    case _ => println("ha what?")
  }
}

Run 起來,結果:

哼
哈
哼
哈
哼
...

遠端示例

local工程

application.conf

akka {
  loglevel = "DEBUG"
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 5155
    }
 }
}
object Local extends App {
  val system = ActorSystem("LocalSystem")
  val localActor = system.actorOf(Props[LocalActor], name = "LocalActor") // the local actor
  localActor ! "START" // start the action
}
class LocalActor extends Actor {
  // create the remote actor
  val remote = context.actorSelection("akka.tcp://[email protected]:5150/user/RemoteActor")
  var counter = 0

  def receive = {
    case "START" =>
      remote ! "Hello from the LocalActor"
    case msg: String =>
      println(s"LocalActor received message: '$msg'")

      if (counter < 5) {
        sender ! "Hello back to you"
        counter += 1
      }
  }
}

remote工程

application.conf

akka {
  loglevel = "DEBUG"
  actor {
    provider = "akka.remote.RemoteActorRefProvider"
  }
  remote {
    enabled-transports = ["akka.remote.netty.tcp"]
    netty.tcp {
      hostname = "127.0.0.1"
      port = 5150
    }
 }
}
object HelloRemote extends App {
  val system = ActorSystem("HelloRemoteSystem")
  val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor")

  remoteActor ! "The RemoteActor is alive"
}
class RemoteActor extends Actor {
  def receive = {
    case msg: String =>
      println(s"RemoteActor received message '$msg'")
      sender ! "Hello from the RemoteActor"
  }
}

原文連結:http://ibruce.info/2014/05/20/hello-akka/