首页  

Akka Quickstart 例子说明     所属分类 akka 浏览量 725
https://developer.lightbend.com/guides/akka-quickstart-scala/index.html

concurrent distributed fault-tolerant event-driven
 
Actors are the unit of execution in Akka

The example consists of three actors:
Greeter:     Receives commands to Greet someone and responds with a Greeted reply to confirm the greeting has taken place
GreeterBot:  Receives the reply from the Greeter and sends a number of additional greeting messages and collect the replies until a given max number of messages have been reached.
GreeterMain: The guardian actor that bootstraps everything


Greet:    command sent to the Greeter actor to greet
Greeted:  reply from the Greeter actor to confirm the greeting has happened
SayHello: command to the GreeterMain to start the greeting process

greeter  迎宾员,接待员 

GreeterMain 创建 ,初始化创建 Greeter 
收到消息时 ,创建 GreeterBot
Greeter 和 GreeterBot 互相发消息


val greeterMain: ActorSystem[GreeterMain.SayHello] = ActorSystem(GreeterMain(), "AkkaQuickStart")
println("greeterMain="+greeterMain+","+LocalDateTime.now())
Thread.sleep(3000)
greeterMain ! SayHello("codefun007")


object GreeterMain {

  def apply(): Behavior[SayHello] =
    Behaviors.setup { context =>
      val greeter = context.spawn(Greeter(), "greeter")
      println("greeter=" + greeter + "," + LocalDateTime.now())
      Behaviors.receiveMessage { message =>
        val replyTo = context.spawn(GreeterBot(max = 3), message.name)
        println("greeter.replyTo.GreeterBot=" + replyTo + "," + LocalDateTime.now())
        greeter ! Greeter.Greet(message.name, replyTo)
        Behaviors.same
      }
    }
  final case class SayHello(name: String)
}


greeterMain=akka://AkkaQuickStart,2022-03-29T08:58:51.001
greeter=Actor[akka://AkkaQuickStart/user/greeter#-533717529],2022-03-29T08:58:51.020
greeter.replyTo.GreeterBot=Actor[akka://AkkaQuickStart/user/codefun007#377111349],2022-03-29T08:58:54.016

注意时间顺序

akka://AkkaQuickStart
akka://AkkaQuickStart/user/greeter
akka://AkkaQuickStart/user/codefun007



Behaviors.receive behavior factory Behaviors.same the next behavior is “the same as the current one” ! operator (pronounced “bang” or “tell”) It is an asynchronous operation that doesn’t block the caller’s thread. greeterMain ! SayHello("codefun007") ActorRef[T] akka.actor.typed.ActorRef An ActorSystem is the intial entry point into Akka. Usually only one ActorSystem is created per application. An ActorSystem has a name and a guardian actor. Spawning child actors val greeter = context.spawn(Greeter(), "greeter") spawn Create a child Actor from the given Behavior and with the given name.
完整例子代码 https://gitee.com/dyyx/hellocode/blob/master/demo/scala/akka/akka-http-quickstart-scala-maven/src/main/java/com/example/QuickstartApp.scala

上一篇     下一篇
scala包对象实例

quarkus 例子项目

idea jdk 版本设置

grpc jar版本不匹配问题处理实例

temporal学习笔记

Temporal Server architecture