Java 喷射scala建筑无阻塞servlet

Java 喷射scala建筑无阻塞servlet,java,scala,servlets,spray,Java,Scala,Servlets,Spray,我已经用akka actor的spray构建了一个scala应用程序 我的问题是请求是同步的,服务器不能同时管理多个请求 这是正常的行为吗?我能做些什么来避免这种情况 这是我的启动代码: object Boot extends App with Configuration { // create an actor system for application implicit val system = ActorSystem("my-service") //context.actorO

我已经用akka actor的spray构建了一个scala应用程序

我的问题是请求是同步的,服务器不能同时管理多个请求

这是正常的行为吗?我能做些什么来避免这种情况

这是我的启动代码:

object Boot extends App with Configuration {

  // create an actor system for application
  implicit val system = ActorSystem("my-service")
//context.actorOf(RoundRobinPool(5).props(Props[TestActor]), "router")
  // create and start property service actor
  val RESTService = system.actorOf(Props[RESTServiceActor], "my-endpoint")

  // start HTTP server with property service actor as a handler
  IO(Http) ! Http.Bind(RESTService, serviceHost, servicePort)
}
演员代码:

class RESTServiceActor extends Actor 
                            with RESTService  {
  implicit def actorRefFactory = context

  def receive = runRoute(rest)
}



trait RESTService extends HttpService  with SLF4JLogging{
  val myDAO = new MyDAO



  val AccessControlAllowAll = HttpHeaders.RawHeader(
    "Access-Control-Allow-Origin", "*"
  )
  val AccessControlAllowHeadersAll = HttpHeaders.RawHeader(
    "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"
  )
  val rest =  respondWithHeaders(AccessControlAllowAll, AccessControlAllowHeadersAll) { 
    respondWithMediaType(MediaTypes.`application/json`){
      options {
            complete {
              ""
            }
          } ~
      path("some"/"path"){
         get {
            parameter('parameter){ (parameter) => 
              ctx: RequestContext =>
                    handleRequest(ctx) {
                      myDAO.getResult(parmeter)
                    }
            }
          }
        } 
    }
  }

    /**
   * Handles an incoming request and create valid response for it.
   *
   * @param ctx         request context
   * @param successCode HTTP Status code for success
   * @param action      action to perform
   */
  protected def handleRequest(ctx: RequestContext, successCode: StatusCode = StatusCodes.OK)(action: => Either[Failure, _]) {
    action match {
      case Right(result: Object) =>
        println(result)
        ctx.complete(successCode,result.toString())
      case Left(error: Failure) =>
      case _ =>
        ctx.complete(StatusCodes.InternalServerError)
    }
  }
}
我看到:

Akka Mist为构建RESTful web提供了极好的基础 Scala中的服务,因为它结合了良好的可伸缩性(通过 异步、非阻塞性),具有一般的轻量级

这就是我错过的吗?spray是使用它作为默认值还是我需要添加它,以及如何添加


我对此有点困惑。非常感谢您的帮助。

如果您是从零开始,我建议您使用Akka HTTP,文档记录在。这是一个喷口,但使用阿克卡溪流,这将是重要的前进


就让代码完全异步而言,关键模式是将
未来
返回到结果,而不是结果数据本身。换句话说,
RESTServiceActor
应该是返回数据的
Future
,而不是实际数据。这将允许Spray/Akka HTTP接受其他连接,服务参与者的异步完成将在完成后返回结果。

而不是将结果发送到complete方法:

ctx.complete(successCode,result.toString())
我使用了未来的方法:

import concurrent.Future
import concurrent.ExecutionContext.Implicits.global

ctx.complete(successCode,Future(Option(result.toString())))

你能告诉我如何根据我的服务参与者代码(包括在问题中)返回一个
未来的
吗?在这个代码中,
myDAO.getResult(参数)
就是实际工作的内容。假设您正在使用类似spray json的东西来序列化结果,那么您将需要使用
Future{myDAO.getResult(parmeter)}
调用
ctx.complete()
。您需要处理错误,例如中所示的模式。Akka HTTP未标记为生产就绪,因此这可能不是最佳选项。@Przemek:任何应用程序使用完毕时,它都将生产就绪。API是最终的,这才是最重要的。对“不接受”感到惊讶的是,大多数人在投稿人引导他们到那里时,会在他们的OP上添加此类内容作为更新部分。