scala - How to manage HTTP requests in Akka? -
i'm using spray in application , examples i've see on github looks people handle http requests in akka passing httpcontext object around actors , calling oncomplete { }
on future in last actor.
is sending context deep down in application idea ? way every event object have context parameter.
how handle http requests & response in akka? i've read this article know people's thoughts run akka in production on right way of achieving this.
i prefer use ask pattern in spray service, , onsuccess directive, e.g.:
trait myservice extends httpservice { def worker: actorref implicit def timeout:timeout implicit def ec:executioncontext def askworker: future[string] = (worker ? "hello").mapto[string] def myroute = path("/") { { onsuccess(askworker){ case str => complete(str) } } } }
then concrete actor such as:
class serviceactor extends myservice actor { implicit val ec = context.system implicit val timeout = timeout(3 seconds) val worker = context.actorof(props[workeractor]) override def actorreffactory = context.system def receive = runroute(myroute) }
i pattern rather passing request context around since means other actors don't have have concept of http. service replaced different protocol. in example worker actor can like:
class workeractor extends actor { def receive = { case "hello" => sender() ! "hello world" } }
Comments
Post a Comment