我在玩Actor Routing,我无法将回复发送回路由器,以便路由列表中的另一个参与者可以接收此消息。我正在使用:
sender.tell([Message], context.parent)
根据akka文档,为了回复路由器,路由参与者将发送者设置为自己,他们的父级是实际的路由器
当回复时,它将在控制台中给出以下消息:
〔信息〕〔12/13/2013 11:19:43.030〕[StarBucks akka.eactor.default-dispatcher-2][akka://StarBucks/deadLetters]消息[net.addictivesoftware.starbucks.MakeCoffee$]来自演员[akka://StarBucks/user/Melanie#-847662818]至演员[akka://StarBucks/deadLetters]未送达。[1] 死亡遇到信件。
主要类别是:
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系统,该系统将把您的邮件重定向到死信邮箱。
编辑:根据文档,请参阅明确说明的路由器、路由器和发送器部分
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.
这正是您的情况,您正在系统参与者下构建员工参与者,然后将ActorRef列表作为的参数传递给路由器的构造函数。