scala akka remote ask 实例
所属分类 akka
浏览量 715
使用 ActorSystem 直接创建 ActorSelection 应用远程actor
val clientActorSystem = ActorSystem("clientActorSystem", conf)
val url = "akka://serverActorSystem@127.0.0.1:8888/user/serverActor"
val remoteActor = clientActorSystem.actorSelection(url)
implicit val timeout: Timeout = Timeout(1, TimeUnit.SECONDS)
val future = remoteActor ? Request("ask hello")
// Future[Any] 转成 Response
future.mapTo[Response].onComplete {
case Success(value) => println(value)
}
val future2 = remoteActor ? Request("ask akka remote")
val result = Await.result(future2, Duration.Inf)
Using ask will send a message to the receiving Actor as with tell,
and the receiving actor must reply with sender() ! reply in order to complete the returned Future with a value.
The ask operation involves creating an internal actor for handling this reply,
which needs to have a timeout after which it is destroyed in order not to leak resources
https://gitee.com/dyyx/hellocode/blob/master/demo/scala/akka/akka-quickstart-scala-maven/src/main/java/com/example/dyyx/remote/ClientSimple.scala
scala akka remote 实例
上一篇
下一篇
Scala 偏函数 例子2
akka typed actor ask 实例
scala akka remote 实例
scala future mapTo 用法
scala强制类型转换
scala成员访问修饰符