我使用Spray和Akka Actor构建了一个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非常适合在Scala中构建RESTful Web服务,因为它将良好的可扩展性(通过异步、非阻塞特性实现)与轻量级结合在了一起。
这是我需要的吗?spray是否默认使用它或者我需要添加它,该怎么做呢?
我有点困惑。任何帮助都将不胜感激。