gpt4 book ai didi

scala - Akka 路由 : Reply's send to router ends up as dead letters

转载 作者:行者123 更新时间:2023-12-02 00:02:20 24 4
gpt4 key购买 nike

我正在玩 Actor Routing,我无法将回复发送回路由器,以便路由列表中的另一个 actor 可以接收到它。我正在使用:

sender.tell([Message], context.parent)

根据 akka 文档回复路由器,路由的 actor 将发件人设置为他们自己,他们的父级是实际的路由器

当回复时,它会在控制台中给出以下信息:

[INFO] [12/13/2013 11:19:43.030] [StarBucks-akka.actor.default-dispatcher-2] [akka://StarBucks/deadLetters] Message [net.addictivesoftware.starbucks.MakeCoffee$] from Actor[akka://StarBucks/user/Melanie#-847662818] to Actor[akka://StarBucks/deadLetters] was not delivered. [1] dead letters encountered.

主类是:

object Starbucks extends App {
implicit val system = ActorSystem.create("StarBucks")

val employees = List(
system.actorOf(Props[Employee], "Penny"),
system.actorOf(Props[Employee], "Leonard"),
system.actorOf(Props[Employee], "Sheldon")
)

val customers = List(
("Raj", "Tall Latte Machiato"),
("Howard", "Double Tall Cappuccino"),
("Bernadette", "Grande Spicy Pumpkin Latte"),
("Amy", "Dopio Espresso")
)

val starBucks = system.actorOf(
Props.empty.withRouter(SmallestMailboxRouter(routees=employees)))

customers foreach { request =>
println("Customer %s orders a %s".format(request._1, request._2))
starBucks ! CanIHave(request._1, request._2)
}
}

路由的actor类是:

class Employee extends Actor {
def receive = {
case CanIHave(coffee, name) => {
println("Employee %s writes '%s' and '%s' on a cup".format(self.path.name, coffee, name) )
sender.tell(MakeCoffee(coffee, name), context.parent)
}
case MakeCoffee(coffee, name) => {
println("Employee %s makes a %s for %s ".format(self.path.name, coffee, name) )
sender.tell(CoffeeReady(coffee, name), context.parent)
}
case CoffeeReady(coffee, name) => {
println("Employee %s shouts: %s for %s is ready!".format(self.path, name, coffee, name))
}
}
}

最佳答案

你的问题是你的路由不是由路由器本身创建的,而是由 Akka 系统创建的:

system.actorOf(Props[Employee], "Penny")

因此,员工级别的 context.parent 将返回 Akka 系统,该系统会将您的消息重定向到死信邮箱。

编辑:根据文档,请参阅 Routers, Routees and Senders 部分明确指出

Note that different code would be needed if the routees were 
not children of the router, i.e. if they were provided when the router was created.

这正是您的情况,您在系统 actor 下构建您的员工 actor,然后将 ActorRef 列表作为 的参数传递给路由器的构造函数。

关于scala - Akka 路由 : Reply's send to router ends up as dead letters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20564381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com