How to respond with the result of an actor call?

Piyush Jajoo

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.

Ramón J Romero y Vigil

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 a HttpResponse based on the HttpRequest. This Actor is asked from within the route.

I have never used Java for routing code so my response is 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 then be used passed into the bindAndHandle method like any other Flow.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to respond to ajax call in Rails

From Dev

Scala Test, how to take a result from an actor

From Dev

How to respond to ajax call from iframe?

From Dev

How to respond correctly to an AJAX call in PHP?

From Dev

How to respond to Sinch call in the background state?

From Dev

How to return respond result to function with Alamofire Swift 3

From Dev

Play 2.4 WebSocket Actor doesn't respond

From Dev

how to get result of an async call?

From Dev

Returning value from actor call

From Dev

How do we send back the result of processed message by actor to the actual sender

From Dev

How can a unique instance of a React component respond to the result of an async action using Flux?

From Dev

How to add to the result of a template call in XSLT

From Dev

How to call studentt method of jstat to get result?

From Dev

How to make a special call and get result in android?

From Dev

How to add to the result of a template call in XSLT

From Dev

How do I call a subclass in an API result?

From Dev

How to call a Stored procedure with aggregate result in output?

From Dev

How to test an actor?

From Dev

How to find an actor in caf

From Dev

How to put actor to sleep?

From Dev

Unable to deserialize ActorRef to send result to different Actor

From Dev

Sending message to child actor on parent and wait result

From Dev

Sending message to child actor on parent and wait result

From Dev

How do I debug a php nusoap call requiring basic authentication that doesn't respond at all?

From Dev

How can I give user choices and respond to them accordingly, in an outbound call via twilio?

From Dev

Android MVVM have view respond to async result

From Dev

How to respond to an XSS attack

From Dev

How to respond to an XSS attack

From Dev

How to respond to clicks in TableView?

Related Related

  1. 1

    How to respond to ajax call in Rails

  2. 2

    Scala Test, how to take a result from an actor

  3. 3

    How to respond to ajax call from iframe?

  4. 4

    How to respond correctly to an AJAX call in PHP?

  5. 5

    How to respond to Sinch call in the background state?

  6. 6

    How to return respond result to function with Alamofire Swift 3

  7. 7

    Play 2.4 WebSocket Actor doesn't respond

  8. 8

    how to get result of an async call?

  9. 9

    Returning value from actor call

  10. 10

    How do we send back the result of processed message by actor to the actual sender

  11. 11

    How can a unique instance of a React component respond to the result of an async action using Flux?

  12. 12

    How to add to the result of a template call in XSLT

  13. 13

    How to call studentt method of jstat to get result?

  14. 14

    How to make a special call and get result in android?

  15. 15

    How to add to the result of a template call in XSLT

  16. 16

    How do I call a subclass in an API result?

  17. 17

    How to call a Stored procedure with aggregate result in output?

  18. 18

    How to test an actor?

  19. 19

    How to find an actor in caf

  20. 20

    How to put actor to sleep?

  21. 21

    Unable to deserialize ActorRef to send result to different Actor

  22. 22

    Sending message to child actor on parent and wait result

  23. 23

    Sending message to child actor on parent and wait result

  24. 24

    How do I debug a php nusoap call requiring basic authentication that doesn't respond at all?

  25. 25

    How can I give user choices and respond to them accordingly, in an outbound call via twilio?

  26. 26

    Android MVVM have view respond to async result

  27. 27

    How to respond to an XSS attack

  28. 28

    How to respond to an XSS attack

  29. 29

    How to respond to clicks in TableView?

HotTag

Archive