2

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

Mutaz
  • 547
  • 4
  • 12
  • see also: http://stackoverflow.com/questions/32036644/how-does-spray-routing-httpservice-dispatch-requests – yǝsʞǝla Nov 03 '15 at 10:05
  • I would have done it a while ago if I was using Spray. It is complicated with akka http. Please take a look at my comments with johny. Thanks – Mutaz Nov 03 '15 at 13:12
  • Could you resolve this issue, I'm struggling with it myself? – Lukasz Jun 25 '16 at 10:36

1 Answers1

1

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.

Johny T Koshy
  • 3,857
  • 2
  • 23
  • 40
  • But then how can I complete the request after `actor ! SomeMessage` result returns? Your last code snippet can't be place inside route directives unless it returns Route (`Future[RouteResult]`). Can you please put it inside Route directives for `GET` request for example? – Mutaz Nov 03 '15 at 12:53
  • If it returns some result, then use `ask` pattern, instead of `tell`, which returns a `Future`. Then you can map that `Future` to a `Future[RouteResult]`. – Johny T Koshy Nov 03 '15 at 12:59
  • 1
    The whole thing is about avoiding `ask` :( I want to make it all with `tell` – Mutaz Nov 03 '15 at 13:10
  • 1
    If you are using `actor ! SomeMessage`, then nothing will return. If that is what you wanted, then you can wrap your response in `Future.successful`. – Johny T Koshy Nov 03 '15 at 13:13
  • This won't work with `tell`. The point is to pass `ctx: RequestContext` to the `PerRequestActor ref` and complete it ( `ctx.complete` ) from within that actor. Would you be able to help with some code to show how I can achieve this in the `route` directives (in my example above) – Mutaz Nov 03 '15 at 13:20
  • I'm struggling with the same issue trying to port the spray code in https://github.com/NET-A-PORTER/spray-actor-per-request to akka http. It seems that the `perRequest` method in the `PerRequest` trait should return a `Future[RouteResult]` which will be completed in the newly created actor. Is the direction of my thinking correct? If so how to achieve that? – The Dude Nov 17 '15 at 12:15
  • @The Dude, which version are you porting to? Op dint want to use `ask pattern`, what exactly is the problem you are facing? You may have to ask a separate question. – Johny T Koshy Nov 17 '15 at 12:31
  • @johny I'm still using `akka-http-experimental` 1.0 (I've seen there is a 2.0-M1 available but it's bit too early for me I guess. I'm actually only starting so maybe it's a good idea to move to the 2.0 asap. What do you think? I've asked a question about tell vs. ask and per-request-actors on the Akka news group. – The Dude Nov 17 '15 at 14:12
  • @The Dude, If you just started learning, use `2.0-M1`. As they are constantly changing signatures, it would be better start with the latest version. By the time you are ready to use it, you wont have a lot to catch up. there are a lot of `tell vs ask` questions here itself. – Johny T Koshy Nov 17 '15 at 14:25