Akka HTTP中每个请求的Actor。如何将它与Route指令集成?

时间:2015-11-03 09:04:20

标签: scala akka akka-http

我努力让这个工作,你能告诉我吗?

def perRequestActor(message: RestMessage): Route = {
  // Compiler error Type mismatch: found (RequestContext) => ActorRef, required Route
  ctx: RequestContext => system.actorOf(propsCreator(ctx, message), "per-req-actor")
}
val route: Route = 
get {
  perRequestActor(new RestMessage("someVal"))
}

如何解决编译器错误并继续使用tell模式来完成请求?请注意我没有使用Spray。使用akka-http

1 个答案:

答案 0 :(得分:1)

方法perRequestActor的返回类型是Route,但是您将返回ActorRef

来自文档

type Route = RequestContext => Future[RouteResult]
     

它是一个函数的简单别名,它将RequestContext作为参数并返回Future [RouteResult]。

     

通常在路由收到请求时(或者更确切地说是RequestContext)   因为它可以做其中一件事:

Complete the request by returning the value of requestContext.complete(...)
Reject the request by returning the value of requestContext.reject(...) (see Rejections)
Fail the request by returning the value of requestContext.fail(...) or by just throwing an exception (see Exception Handling)
Do any kind of asynchronous processing and instantly return a Future[RouteResult] to be eventually completed later on

对于使用tell模式,您可以向您创建的actor发送消息。

val actor = system.actorOf(propsCreator(ctx, message), "per-req-actor")
actor ! SomeMessage

akka-http的最新版本(1.0)的文档更清晰,签名略有不同。如果你刚开始学习,你应该使用它。