Akka路由:回复发送到路由器的结果是死信 [英] Akka Routing: Reply's send to router ends up as dead letters

查看:277
本文介绍了Akka路由:回复发送到路由器的结果是死信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Actor路由,因此无法将回复发送回路由器,以便路由列表中的另一个Actor可以接听。
我正在使用:

I'm playing around with Actor Routing and I can't send a reply back to the router so that another actor in the routing list can pick this up. I'm using:

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

根据akka docs来答复路由器,被路由的参与者将发送者设置为自己,而其父节点是实际的路由器

to reply to the router as according to akka docs, routed actors set the sender to themselves and their parent is the actual router

回复时会在控制台中显示以下消息:

when replying it will give the following message in the console:


[INFO ] [12/13/2013 11:19:43.030]
[StarBucks-akka.actor.default-dispatcher-2]
[akka:// StarBucks / deadLetters]消息
[net从
Actor [akka:// StarBucks / user / Melanie#-847662818]到
Actor [akka:// StarBucks / deadLetters]的.addictivesoftware.starbucks.MakeCoffee $]未交付。 [1]遇到死
个字母。

[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)
  }
}

路由的演员类别为:

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系统使用:

Your problem is that your routees are not created by the router itself but by the Akka system with :

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

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

Therefore context.parent at the employee level will return the Akka system which will redirect your messages to the dead-letters mailbox.

编辑:根据文档,请参阅路由器,路由和发件人明确指出

EDIT : According the documentation, see the section Routers, Routees and Senders which states explicitly

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列表作为路由器构造函数的参数。

This is exactly your situation, you are building your employees actors under the system actor and then you pass the ActorRef list as an argument of to the router's constructor.

这篇关于Akka路由:回复发送到路由器的结果是死信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆