Akka-Http:如何在请求中使用Actor?

时间:2016-05-26 13:46:51

标签: scala akka akka-http

基于Akka-HTTP的简单代码:

val route =
    pathPrefix("myapp") {
      path("search") {
        get {
          //ref ! DoSomething("foo")
          complete(HttpEntity(ContentTypes.`application/json`, /* content here from an actor */ ))
        }
      }
    }

如何从Actor(sender ! content)返回值?

1 个答案:

答案 0 :(得分:4)

使用ask模式并映射它的回归未来。

import akka.pattern.ask    // enable `?`
import context.dispatcher  // Future's need an execution context, we use the Actor#context's one 

(ref ? DoSomething("foo")).mapTo[ReturningType].map { result =>
  complete(HttpEntity(ContentTypes.`application/json`, result ))
}