Akka http - handle connections entirely with an actor

260 views
Skip to first unread message

Dana Peele

unread,
May 11, 2015, 9:55:22 AM5/11/15
to akka...@googlegroups.com
I've built a simple server using the http library as follows:


class ResponseActor extends Actor {
  def receive = {
    case _ => sender ! HttpResponse(entity = HttpEntity(MediaTypes.`text/html`, "<html><body>Hello!</body></html>"))
  }
}

object ReactiveInventoryService extends App {
  implicit val system = ActorSystem()
  implicit val executor = system.dispatcher
  implicit val materializer = ActorFlowMaterializer()
  implicit val timeout = Timeout(5 seconds)

  val config = ConfigFactory.load()

  val responseHandler = system.actorOf(Props[ResponseActor])

  val requestHandler: HttpRequest => Future[HttpResponse] = {
    httpRequest: HttpRequest => ask(responseHandler, httpRequest).mapTo[HttpResponse]
  }

  val serverBinding: Source[IncomingConnection, Future[ServerBinding]] = Http().bind(config.getString("http.interface"), config.getInt("http.port"))
  serverBinding.runForeach { connection: IncomingConnection =>
    println("Accepted new connection from " + connection.remoteAddress)
    connection handleWithAsyncHandler requestHandler
  }
}


My only issue is this line:

httpRequest: HttpRequest => ask(responseHandler, httpRequest).mapTo[HttpResponse]

What do I need to do to give the IncomingConnection to an actor and have it send back the response without having to use ask and return the HttpResponse back to the request handler?

Thanks!

Dana

Martynas Mickevičius

unread,
May 16, 2015, 4:32:53 AM5/16/15
to akka...@googlegroups.com
Hi Dana,


In summary, you can use completeWith directive.

--
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.



--
Martynas Mickevičius
TypesafeReactive Apps on the JVM

Dana Peele

unread,
May 19, 2015, 10:40:47 PM5/19/15
to akka...@googlegroups.com
Ahh, this is perfect! Thank you Martynas.
Message has been deleted

Mutaz Qasem

unread,
Dec 20, 2015, 11:15:30 AM12/20/15
to Akka Developer List
How can I access RequestContext when I use completeWith? I need to pass it along to my actor that is completing the request.

Thanks

Konrad Malawski

unread,
Dec 23, 2015, 5:09:31 PM12/23/15
to akka...@googlegroups.com, Mutaz Qasem
Yes, you would need to pass it along to the actor.

-- 
Cheers,
Konrad 'ktoso’ Malawski
Akka @ Typesafe

Mutaz Qasem

unread,
Dec 23, 2015, 11:46:18 PM12/23/15
to Konrad Malawski, akka...@googlegroups.com
But how can I pass it?

Thanks
Reply all
Reply to author
Forward
0 new messages