Akka路由:回复发送到路由器最终为死信

问题描述 投票:3回答:1

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

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

根据akka docs回复路由器,路由的actor将发送者设置为自己,父母是实际的路由器

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

[INFO] [12/13/2013 11:19:43.030] [StarBucks-akka.actor.default-dispatcher-2] [akka:// StarBucks / deadLetters]来自Actor的消息[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))
    }
  }
}
scala akka actor
1个回答
3
投票

您的问题是您的路由不是由路由器本身创建的,而是由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下构建您的employees actor,然后将ActorRef列表作为路由器构造函数的参数传递。

© www.soinside.com 2019 - 2024. All rights reserved.