Akka-Http:如何 return 来自演员的回应?
Akka-Http: How to return a response from an actor?
我在具有 "ask" 模式的请求中使用演员:
val route =
pathPrefix("myapp") {
path("search") {
get {
(mainActorRef ? DoSomething("foo")).mapTo[ReturningType].map { result =>
complete(HttpEntity(ContentTypes.`application/json`, result ))
}
}
}
}
问题是主要演员与其他演员交流并从其中一位演员那里得到答复,如下所示:
class MainActor extends Actor {
override def receive: Receive = {
case d:DoSomething =>
anotherActor ! DoThis(d)
// received from anotherActor as reply to DoThis
case r:DoThisResponse =>
// how do I send the response back to my “route”?
pipe (Future{r}) to ???
}
}
如何将此答案作为响应发送回 Akka-Http?
在主角中使用 "sender()" 无效,因为它不是正确的参考。我应该在主要演员内部传递 DoSomething
一些与 "tell" (!) 一起使用的参考吗?我如何传递此引用?
发送到 anotherActor
时,在 MainActor
中使用 forward
而不是 tell
。这样 anotherActor
就不会 "see" MainActor
作为发件人。
所以,基本上,您在中间步骤中使用 forward
发送新消息,但是行中的参与者可以简单地响应 sender
,因为它看不到中间参与者。
编辑:完整MainActor
class MainActor extends Actor {
override def receive: Receive = {
//delegating some more work to another container
case d:DoSomething =>
anotherActor forward DoThis(d)
// sending a response back to "route"
case r:DoThisResponse =>
sender ! Response
}
}
我在具有 "ask" 模式的请求中使用演员:
val route =
pathPrefix("myapp") {
path("search") {
get {
(mainActorRef ? DoSomething("foo")).mapTo[ReturningType].map { result =>
complete(HttpEntity(ContentTypes.`application/json`, result ))
}
}
}
}
问题是主要演员与其他演员交流并从其中一位演员那里得到答复,如下所示:
class MainActor extends Actor {
override def receive: Receive = {
case d:DoSomething =>
anotherActor ! DoThis(d)
// received from anotherActor as reply to DoThis
case r:DoThisResponse =>
// how do I send the response back to my “route”?
pipe (Future{r}) to ???
}
}
如何将此答案作为响应发送回 Akka-Http?
在主角中使用 "sender()" 无效,因为它不是正确的参考。我应该在主要演员内部传递 DoSomething
一些与 "tell" (!) 一起使用的参考吗?我如何传递此引用?
发送到 anotherActor
时,在 MainActor
中使用 forward
而不是 tell
。这样 anotherActor
就不会 "see" MainActor
作为发件人。
所以,基本上,您在中间步骤中使用 forward
发送新消息,但是行中的参与者可以简单地响应 sender
,因为它看不到中间参与者。
编辑:完整MainActor
class MainActor extends Actor {
override def receive: Receive = {
//delegating some more work to another container
case d:DoSomething =>
anotherActor forward DoThis(d)
// sending a response back to "route"
case r:DoThisResponse =>
sender ! Response
}
}