Mixing akka-http routing with actors

1,557 views
Skip to first unread message

Brian Topping

unread,
Oct 28, 2014, 11:40:21 PM10/28/14
to akka...@googlegroups.com
Hi all,

I'd like to use the routing DSL to wrap a call to an actor. This used to be dirt simple, just send a message of "_" to the actor. Things are different now.

Here is the template of the actor-less server:

    val bindingFuture = (IO(Http) ? Http.Bind(interface = "0.0.0.0", port = 9000)).mapTo[Http.ServerBinding]
handleConnections(bindingFuture) withRoute {
get {
pathPrefix("document" / Segment) {
docId => {
            // what goes here?
}
}
}
}

Trivially, with the "actor" consuming a "case class Message" and returning a "case class Result(s: String)" I believe it looks something like this:

    val bindingFuture = (IO(Http) ? Http.Bind(interface = "0.0.0.0", port = 9000)).mapTo[Http.ServerBinding]
handleConnections(bindingFuture) withRoute {
get {
pathPrefix("document" / Segment) {
docId => {
            Future[RouteResult] {
actor ? Message onComplete {
case Success(Result(s)) => Complete(HttpResponse(200, s))
}
            }           
          }
        }
}
}

In reality, much missing. Could I beg some pointers from the oracles?

Cheers, Brian

Johannes Rudolph

unread,
Oct 29, 2014, 4:11:20 AM10/29/14
to akka...@googlegroups.com
Hi Brian,

everything is now Future-based instead of relying on RequestContext's
side-effects as in spray. Apart from that, not much changed. So, just
use the Akka ask pattern and either A) complete with the future if you
have a Marshaller available or otherwise B) use the `onComplete` or
`onSuccess` directives to continue working with the result of the
Future:

A)

pathPrefix("document" / Segment) {
docId => complete((actor ? Message).mapTo[Result])
}

B)

pathPrefix("document" / Segment) {
docId =>
onSuccess((actor ? Message).mapTo[Result]) { (r: Result) =>
// put another route here
}
}

Both would have worked the same way in spray.

HTH
Johannes
> --
> You received this message because you are subscribed to the Google Groups
> "Akka Developer List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to akka-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net

Brian Topping

unread,
Oct 29, 2014, 5:16:58 PM10/29/14
to akka...@googlegroups.com
Got it, thanks!

Is there a way to do this that doesn't include using "complete"? For instance, if I wanted to get access to the request and response objects. Maybe that's not realistic inside the DSL.

Cheers, Brian

Mathias Doenitz

unread,
Oct 30, 2014, 5:46:52 AM10/30/14
to akka...@googlegroups.com
Brian,

the DSL is flexible enough to enable access to anything you want… :)
So, if you want to get a hold of the domain object that is marshalled into a response, no problem (one way would be the `onSuccess` directive).
If you want to get a hold of the HttpResponse produced by marshalling your domain response object, that’s also possible.

What exactly are you trying to do?

Cheers,
Mathias

---
mat...@spray.io
http://spray.io

Mutaz Qasem

unread,
Oct 26, 2015, 11:28:19 AM10/26/15
to Akka Developer List
I'm still not able to understand why this doesn't work in the routing DSL

future onComplete {
case Success(result) => complete(result) //complete HTTP response
case Failure(failure) => complete(failure)
}

OR

future onSuccess {
case res => complete(res)
}
future onFailure {
case failure => complete(failure)
}

But anyway, my question is, how can I handle failure and send a failure message to the client? I can find only onSuccess in akka.http.scaladsl.server.Directives . Is there a way to have onFailure?

Reply all
Reply to author
Forward
0 new messages