日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Akka DEMO

發布時間:2024/6/18 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Akka DEMO 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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://HelloRemoteSystem@127.0.0.1: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 actorlocalActor ! "START" // start the action } class LocalActor extends Actor {// create the remote actorval remote = context.actorSelection("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")var counter = 0def 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"} }

版權聲明:本文為博主原創文章,未經博主允許不得轉載。

轉載于:https://www.cnblogs.com/stark-summer/p/4829785.html

總結

以上是生活随笔為你收集整理的Akka DEMO的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。