We are looking at using Akka-HTTP Java API - using Routing DSL.
It's not clear how to use the Routing functionality to respond to an HttpRequest; using an Untyped Akka Actor. For example, upon matching a Route path, how do we hand off the request to a "handler" ActorRef, which will then respond with a HttpResponse in a asynchronous way?
A similar question was posted on Akka-User mailing list, but with no followup solutions as such - https://groups.google.com/d/msg/akka-user/qHe3Ko7EVvg/KC-aKz_o5aoJ.
This can be accomplished with a combination of the
onComplete
directive and the ask pattern.In the below example the
RequestHandlerActor
actor is used to create aHttpResponse
based on theHttpRequest
. This Actor is asked from within the route.I have never used Java for routing code so my response is in Scala.
This route can then be used passed into the
bindAndHandle
method like any other Flow.