Scala 在RondRobinPool中找到选定的演员?
如果我有一个像这样的圆形机器人Scala 在RondRobinPool中找到选定的演员?,scala,akka,akka-actor,Scala,Akka,Akka Actor,如果我有一个像这样的圆形机器人 val-actorPoolRef=AkkaConfig.actorSystem.actorOf(RoundRobinPool(100.props)(props[MyService])) 还有一个处理者 def requestHandler(请求:HttpRequest):未来[HttpResponse]={ val promise=promise[HttpResponse]() 承诺。完成(actorPoolRef?请求) 前途 } 我有办法吗 从def r
val-actorPoolRef=AkkaConfig.actorSystem.actorOf(RoundRobinPool(100.props)(props[MyService]))
还有一个处理者
def requestHandler(请求:HttpRequest):未来[HttpResponse]={
val promise=promise[HttpResponse]()
承诺。完成(actorPoolRef?请求)
前途
}
我有办法吗
- 从
、或def requestHandler
- 向刚刚处理请求的同一参与者发送后续消息
akka.pattern.ask
从参与者的RoundRobinPool
请求参与者引用,将self.path
作为选项返回,并将响应包装为选项[ActorPath]
。为了澄清我所说的,我构建了这个简单的概念证明:
import akka.actor.{Actor, ActorLogging, ActorPath, ActorSystem, Props}
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.pattern.ask
import akka.routing.RoundRobinPool
import akka.util.Timeout
import scala.concurrent.Future
import scala.concurrent.duration._
object BasicRoundRobinHttpServer {
def main(args: Array[String]): Unit = {
run()
}
def run() = {
implicit val system = ActorSystem("BasicRoundRobinHttpServer")
import system.dispatcher
implicit val timeout = Timeout(3 seconds)
val myServiceActor = system.actorOf(RoundRobinPool(5).props(Props[MyService]), "myServiceActor")
val simpleRoute: Route =
(path("reference") & get) {
val validResponseFuture: Option[Future[HttpResponse]] = {
// construct the HTTP response
val actorPathResponse: Future[Option[ActorPath]] = (myServiceActor ? "reference").mapTo[Option[ActorPath]]
Option(actorPathResponse.map(ref => HttpResponse(
StatusCodes.OK,
entity = HttpEntity(
ContentTypes.`text/html(UTF-8)`,
s"""
|<html>
| <body>I got the actor reference: ${ref} </body>
|</html>
|""".stripMargin
))))
}
val entityFuture: Future[HttpResponse] = validResponseFuture.getOrElse(Future(HttpResponse(StatusCodes.BadRequest)))
complete(entityFuture)
}
println("http GET localhost:8080/reference")
Http().newServerAt("localhost", 8080).bind(simpleRoute)
}
}
class MyService extends Actor with ActorLogging {
override def receive: Receive = {
case "reference" =>
log.info(s"request reference at actor: ${self}")
sender() ! Option(self.path)
case message =>
log.info(s"unknown message: ${message.toString}")
}
}
// first time
<html>
<body>I got the actor reference: Some(akka://BasicRoundRobinHttpServer/user/myServiceActor/$a) </body>
</html>
// second time
<html>
<body>I got the actor reference: Some(akka://BasicRoundRobinHttpServer/user/myServiceActor/$b) </body>
</html>
...