akka typed actor 实例之 echo
所属分类 akka
浏览量 722
Classic Actors
https://doc.akka.io/docs/akka/current/actors.html
Akka Classic pertains to the original Actor APIs, which have been improved by more type safe and guided Actor APIs.
Akka Classic is still fully supported and existing applications can continue to use the classic APIs.
It is also possible to use the new Actor APIs together with classic actors in the same ActorSystem
typed actor
https://doc.akka.io/docs/akka/current/typed/actors.html
clientActor 向 serverActor 发送消息
serverActor 收到消息后 向 clientActor 回应消息
package com.example
import akka.actor.typed.scaladsl.Behaviors
import akka.actor.typed.{ActorRef, ActorSystem, Behavior}
final case class EchoMsg(msg: String, sender: ActorRef[EchoMsg])
object ServerActor {
def apply(): Behavior[EchoMsg] = Behaviors.receive { (context, message) =>
println("server receive msg " + message.msg + ",sender=" + message.sender + ",self=" + context.self)
message.sender ! EchoMsg(message.msg, context.self)
Behaviors.same
}
}
object ClientActor {
def apply(): Behavior[EchoMsg] = Behaviors.receive { (context, message) =>
println("client receive msg " + message.msg + ",sender=" + message.sender + ",self=" + context.self)
Behaviors.same
}
}
object EchoTypedActorMain extends App {
val serverActor: ActorSystem[EchoMsg] = ActorSystem(ServerActor(), "serverActor")
val clientActor: ActorSystem[EchoMsg] = ActorSystem(ClientActor(), "clientActor")
val list = List("hello", "akka", "typed", "actor")
list.foreach { str =>
serverActor ! EchoMsg(str, clientActor)
println()
Thread.sleep(1000)
}
}
完整代码
https://gitee.com/dyyx/hellocode/blob/master/demo/scala/akka/akka-quickstart-scala-maven/src/main/java/com/example/EchoTypedActor.scala
akka actor demo
akka actor ask 模式 实例
akka typed actor 实例之hello
上一篇
下一篇
Scala foldLeft foldRight
play json库简介
akka typed actor 实例之hello
scala技术栈新人学习指南
Scala 偏函数 例子1
Scala 偏函数 例子2