Spray scala building not blocking servlet
I created a scala app using an acc-actor spray.
My problem is that the request is synchronized and the server cannot manage many requests at once.
Is this normal behavior? what can i do to avoid this?
This is my boot code:
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)
}
actor code:
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)
}
}
}
I saw that:
Akka Mist provides a great foundation for building a RESTful services website in scala as it combines good scalability (asynchronous, non-blocking nature included) with overall ease
Is this what I am missing? spray using it by default or do I need to add it and how?
I'm a little embarrassed. any help is appreciated.
source to share
If you are starting from scratch, I suggest using Akka HTTP documented at http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0-M4/scala/http/ . This is the Spray port, but using Akka Streams, which will be essential to move forward.
As far as your code is completely asynchronous, the key pattern should return Future
to your result, not the result data itself. In other words, there RESTServiceActor
should be a return Future
that returns data, not actual data. This will allow Spray / Akka HTTP to accept additional connections, and asynchronously completing the service actor action will return results when they are complete.
source to share