基于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
)返回值?
答案 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 ))
}