Max*_*Max 5 scala actor akka spray
之后玩过Play!框架一段时间,我先看看Spray.我从我在GitHub上找到的一个样本开始,现在我想修改它但是我不容易理解它是如何工作的.
我如何在下面的代码中等待来自Actor的消息?
package api
import akka.actor.ActorRef
import scala.concurrent.ExecutionContext
import spray.routing.Directives
import core.ClassifierActor
class ClassifierService(classifier: ActorRef)(implicit executionContext: ExecutionContext)
extends Directives with DefaultJsonFormats {
import ClassifierActor._
implicit val classifyMessageFormat = jsonFormat4(ClassifyMessage)
val route =
path("classify") {
post {
handleWith {
// The ClassifierActor gets a ClassifyMessage and
// sends a ClassifiedMessage back to the sender.
// How can wait for the ClassifiedMessage here
// and send a HttpResponse back?
cm: ClassifyMessage => classifier ! cm
// ???
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
hel*_*ser 13
喷雾已经基于akka.io
因此,如果您只想通过actor响应完成路线,则可以使用ask模式
import akka.pattern.ask
import scala.concurrent.duration._
implicit val timeout = Timeout(5 seconds) // needed for `?` below
val route =
path("classify") {
post {
onComplete(actorResponse(yourActor, yourMessage)) {
complete(_)
}
}
}
def actorResponse[T](actor: ActorRef, msg: ClassifyMessage): Future[T] =
(actor ? msg).mapTo[T]
Run Code Online (Sandbox Code Playgroud)
如果要将请求转发给actor模型并在actor系统中的某个地方完成路由,则需要将RequestContext转发给actor.也许,这个例子可以帮助你.祝好运!
| 归档时间: |
|
| 查看次数: |
2889 次 |
| 最近记录: |