Mutaz
Mutaz

Reputation: 547

Actor per request in Akka HTTP. How to integrate it with Route directives?

I'm struggling to get this working, can you please advise?

def perRequestActor(message: RestMessage): Route = {
  // Compiler error Type mismatch: found (RequestContext) => ActorRef, required Route
  ctx: RequestContext => system.actorOf(propsCreator(ctx, message), "per-req-actor")
}
val route: Route = 
get {
  perRequestActor(new RestMessage("someVal"))
}

How can I resolve that compiler error and keep using the tell pattern to complete the request? Please note I'm not using Spray. Using akka-http

Upvotes: 2

Views: 1689

Answers (1)

Johny T Koshy
Johny T Koshy

Reputation: 3887

The return type of the method perRequestActor is Route, but you are returning ActorRef.

From documentation

type Route = RequestContext => Future[RouteResult]

It's a simple alias for a function taking a RequestContext as parameter and returning a Future[RouteResult].

Generally when a route receives a request (or rather a RequestContext for it) it can do one of these things:

Complete the request by returning the value of requestContext.complete(...)
Reject the request by returning the value of requestContext.reject(...) (see Rejections)
Fail the request by returning the value of requestContext.fail(...) or by just throwing an exception (see Exception Handling)
Do any kind of asynchronous processing and instantly return a Future[RouteResult] to be eventually completed later on

For using tell pattern you can send messages to the actor that you created.

val actor = system.actorOf(propsCreator(ctx, message), "per-req-actor")
actor ! SomeMessage

Documentation for the latest version(1.0) of akka-http is more clear and has a slightly different signature. If you just started to learn you should probably use that.

Upvotes: 1

Related Questions