java - How to respond with the result of an actor call? -
we looking @ using akka-http java api - using routing dsl.
it's not clear how use routing functionality respond httprequest; using untyped akka actor. example, upon matching route path, how hand off request "handler" actorref, respond httpresponse in asynchronous way?
a similar question posted on akka-user mailing list, no followup solutions such - https://groups.google.com/d/msg/akka-user/qhe3ko7evvg/kc-akz_o5aoj.
this can accomplished combination of oncomplete
directive , ask pattern.
in below example requesthandleractor
actor used create httpresponse
based on httprequest
. actor asked within route.
i have never used java routing code response in scala.
import scala.concurrent.duration._ import akka.actor.actorsystem import akka.http.scaladsl.model.httpresponse import akka.http.scaladsl.model.httprequest import akka.actor.actor import akka.http.scaladsl.server.directives._ import akka.actor.props import akka.pattern.ask import akka.util.timeout import scala.util.{success, failure} import akka.http.scaladsl.model.statuscodes.internalservererror class requesthandleractor extends actor { override def receive = { case httprequest : httprequest => sender() ! httpresponse(entity = "actor responds nicely") } } implicit val actorsystem = actorsystem() implicit val timeout = timeout(5 seconds) val requestref = actorsystem actorof props[requesthandleractor] val route = extractrequest { request => oncomplete((requestref ? request).mapto[httpresponse]) { case success(response) => complete(response) case failure(ex) => complete((internalservererror, s"actor not playing nice: ${ex.getmessage}")) } }
this route can used passed bindandhandle
method other flow.
Comments
Post a Comment