我正在使用Akka Cluster(版本2.4.10),其中几个节点被指定用于"前端"角色和其他几个人作为"工人"。工人在远程机器上。传入的工作由前端角色通过循环路由分配给工作人员。问题是回复工人"回到前端演员。我可以看到这项工作正在由工人完成。但工人发送到前端的消息并没有达到并最终成为死信。我在日志中看到以下错误。
[Cluster-akka.actor.default-dispatcher-21] [akka://Cluster/deadLetters] Message [scala.collection.immutable.$colon$colon] from Actor[akka://Cluster/user] to Actor[akka://Cluster/deadLetters] was not delivered. [6] dead letters encountered.
我见过this,我的代码也是如此。我也看过this,但建议的解决方案不适用于这种情况,因为我不知道前面的路由。它来自配置,它可以改变。循环路由器配置如下。
akka.actor.deployment {
/frontEnd/hm = {
router = round-robin-group
nr-of-instances = 5
routees.paths = ["/user/hmWorker"]
cluster {
enabled = on
use-role = backend
allow-local-routees = on
}
}
}
路由器在前端actor中实例化,如下所示。
val router = context.actorOf(FromConfig.props(), name = "hm")
val controller = context.actorOf(Props(classOf[Controller], router))
控制器和工人代码在下面。
// Node 1 : Controller routes requests using round-robin
class Controller(router: ActorRef) extends Actor {
val list = List("a", "b") // Assume this is a big list
val groups = list.grouped(500)
override def receive: Actor.Receive = {
val futures = groups.map(grp => (router ? Message(grp)).mapTo[List[String]]))
val future = Future.sequence(futures).map(_.flatten)
val result = Await.result(future, 50 seconds)
println(s"Result is $result")
}
}
// Node 2
class Worker extends Actor {
override def receive: Actor.Receive = {
case Message(lst) =>
val future: Future[List[String]] = // Do Something asynchronous
future onComplete {
case Success(r) => sender.!(r)(context.parent) // This message is not delivered to Controller actor.
case Failure(th) => // Error handling
}
}
}
请告诉我这里我做错了什么。感谢您的帮助。
答案 0 :(得分:2)
您不应在sender()
的回调中使用Future
。在处理回调时,sender()
可能指的是与收到消息时不同的内容。
首先考虑在回调之外保存引用,如:
override def receive: Actor.Receive = {
case Message(lst) =>
val future: Future[List[String]] = // Do Something asynchronous
val replyTo: ActorRef = sender()
future onComplete {
case Success(r) => replyTo.!(r)(context.parent) // This message is not delivered to Controller actor.
case Failure(th) => // Error handling
}
}
甚至更好,使用管道模式:
import akka.pattern.pipe
override def receive: Actor.Receive = {
case Message(lst) =>
val future: Future[List[String]] = // Do Something asynchronous
future.pipeTo(sender())
}