java - Spray scala building non blocking servlet -
i've builded scala application using spray akka actor.
my problem request synchronized , server can't manage many requests @ once.
is normal behaviour? can avoid this?
this boot code:
object boot extends app configuration { // create actor system application implicit val system = actorsystem("my-service") //context.actorof(roundrobinpool(5).props(props[testactor]), "router") // create , start property service actor val restservice = system.actorof(props[restserviceactor], "my-endpoint") // start http server property service actor handler io(http) ! http.bind(restservice, servicehost, serviceport) }
actor code:
class restserviceactor extends actor restservice { implicit def actorreffactory = context def receive = runroute(rest) } trait restservice extends httpservice 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"){ { parameter('parameter){ (parameter) => ctx: requestcontext => handlerequest(ctx) { mydao.getresult(parmeter) } } } } } } /** * handles incoming request , create valid response it. * * @param ctx request context * @param successcode http status code success * @param action action 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 excellent basis building restful web services in scala since combines scalability (enabled asynchronous, non-blocking nature) general lightweight-ness
is i'm missing? spray using default or need add it, , how?
i'm bit confuse it. appreciated.
if starting scratch, suggest using akka http, documented @ http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0-m4/scala/http/. port of spray, using akka streams, important moving forward.
as far making code asynchronous, key pattern return future
result, not result data itself. in other words, restserviceactor
should return future
returns data, not actual data. allow spray/akka http accept additional connections , asynchronous completion of service actor return results when finished.
Comments
Post a Comment