使用akka框架

环境 scala sbt
scala是一种运行在jvm平台的一门多范式的编程语言
sbt是简单构建工具(Simple build tool)的缩写,可以从官网获取
akka actor是typesafe(lightbend前身)公司出品的一个Actor框架

创建akka-demo文件夹,创建build.sbt并添加以下代码

1
2
3
4
5
6
7
8
name := "akka-demo"
version := "0.1"
scalaVersion := "2.12.8"
lazy val akkaVersion = "2.5.20"
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-testkit" % akkaVersion,
)

执行sbt命令等待完成

编写程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.inurl.akka

import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Props}

/**
* @author raylax
*/
class Greeter(message: String, printerActor: ActorRef) extends Actor {
import Greeter._
import Printer._

var greeting = ""

def receive = {
// 保存greeting
case WhoToGreet(who) =>
greeting = message + ", " + who
// 向printerActor发送Greeting消息
case Greet =>
printerActor ! Greeting(greeting)
}
}

object Greeter {
def props(message: String, printerActor: ActorRef): Props = Props(new Greeter(message, printerActor))
final case class WhoToGreet(who: String)
case object Greet
}


class Printer extends Actor with ActorLogging {
import Printer._

def receive = {
// 打印greeting
case Greeting(greeting) =>
log.info("Greeting received (from " + sender() + "): " + greeting)
}
}
object Printer {
def props: Props = Props[Printer]
final case class Greeting(greeting: String)
}

object ActorDemo extends App {
import Greeter._

// 创建Actor系统
val system: ActorSystem = ActorSystem("helloAkka")

// 创建子Actor对象
val printer: ActorRef =
system.actorOf(Printer.props, "printerActor")
val howdyGreeter: ActorRef =
system.actorOf(Greeter.props("Howdy", printer), "howdyGreeter")
val helloGreeter: ActorRef =
system.actorOf(Greeter.props("Hello", printer), "helloGreeter")
val goodDayGreeter: ActorRef =
system.actorOf(Greeter.props("Good day", printer), "goodDayGreeter")

// 发送消息
howdyGreeter ! WhoToGreet("Akka")
howdyGreeter ! Greet

howdyGreeter ! WhoToGreet("Lightbend")
howdyGreeter ! Greet

helloGreeter ! WhoToGreet("Scala")
helloGreeter ! Greet

goodDayGreeter ! WhoToGreet("Play")
goodDayGreeter ! Greet

}

执行程序输出内容

1
2
3
4
[INFO] [02/12/2019 17:03:28.281] [helloAkka-akka.actor.default-dispatcher-5] [akka://helloAkka/user/printerActor] Greeting received (from Actor[akka://helloAkka/user/howdyGreeter#-1189614707]): Howdy, Akka
[INFO] [02/12/2019 17:03:28.293] [helloAkka-akka.actor.default-dispatcher-2] [akka://helloAkka/user/printerActor] Greeting received (from Actor[akka://helloAkka/user/helloGreeter#-140442030]): Hello, Scala
[INFO] [02/12/2019 17:03:28.293] [helloAkka-akka.actor.default-dispatcher-2] [akka://helloAkka/user/printerActor] Greeting received (from Actor[akka://helloAkka/user/howdyGreeter#-1189614707]): Howdy, Lightbend
[INFO] [02/12/2019 17:03:28.297] [helloAkka-akka.actor.default-dispatcher-7] [akka://helloAkka/user/printerActor] Greeting received (from Actor[akka://helloAkka/user/goodDayGreeter#1061792007]): Good day, Play