How to manage HTTP requests in Akka?

I use Spray in my application and from the examples that I see on Github, it looks like people are processing HTTP requests in Akka, passing the HTTPContext object to all participants and calling onComplete { }in the future to the last actor.

Is sending context to the app really a good idea? Thus, each event object will have a context parameter.

How do we handle HTTP requests and response correctly in Akka? I read this article , but I would like to know the people who control Akka in the production process on the right track to achieve this.

+4
source share
1 answer

Spray onSuccess, :

trait MyService extends HttpService {

  def worker: ActorRef

  implicit def timeout:Timeout

  implicit def ec:ExecutionContext

  def askWorker: Future[String] = (worker ? "Hello").mapTo[String]

  def myRoute = path("/") {
    get {
      onSuccess(askWorker){
        case str => complete(str)
      }
    }
  }
}

, :

class ServiceActor extends MyService with Actor {

  implicit val ec = context.system

  implicit val timeout = Timeout(3 seconds)

  val worker = context.actorOf(Props[WorkerActor])

  override def actorRefFactory = context.system

  def receive = runRoute(myRoute)
}

, , , - Http. . :

class WorkerActor extends Actor {

  def receive = {
    case "Hello" => sender() ! "Hello World"
  }
}
+4

Source: https://habr.com/ru/post/1611074/


All Articles