12

Would you know a nice way in Akka/Scala to add/remove routees to a Broadcaster router?

I have been looking at the Resizer - but does not meet my needs (I cannot see how to explicitly ask the resizer to resize (add routees) and to remove it seems that you need to send a Poison Pill to the actor which is then removed).

So far, I have a router with a list of ActorRef and I send AddRoutee and RemoveRoutee messages....

My business case: I have an actor getting data from the network (via a proxy), and it needs to dispatch this data to independent actors for processing in parallel. Due to the graph nature of the recipients (DAG), the graph can evolve at runtime, vertices/edges being modified, hence the need to add and remove routees

There must be a cleaner way to do this.

Thanks for the pointers.

Example of code I would like Akka to handle:

 class MDActor extends Actor {
  @volatile var routees = Set[ActorRef]()

  def receive = {
    case ar: AddRoutee => routees = routees + ar.actorRef
    case rr: RemoveRoutee => routees = routees - rr.actorRef
    case msg => routees.foreach(r => r forward msg)
  }
}
4

2 回答 2

2

每当您发现自己在路由器中缺少某个功能时,这是开始从另一个方向思考的好时机:您提供的参与者代码有什么问题?除非您需要每秒路由超过几百万条消息(根据您的描述,这不太可能),否则这样的参与者正是正确的解决方案。路由器是一种非常专业的结构,不应用作替代品;仅在它们完全满足您的要求并且您已经对普通参与者进行基准测试时才使用它们。

于 2013-05-22T16:50:51.903 回答
1

不确定是否有比保持路由状态并在每次更改时构建新路由器或不使用路由器并使用普通参与者更好的方法。我最近也在看这个。

支持不变性 - 那么,首选的解决方案很可能是丢弃旧的路由器和/或集合并构建一个新的(路由器或参与者集/映射)。

您可以只跟踪您的参与者而不使用路由器 - 这是一个很好的解决方案,并在 akka 文档中推荐为路由器的更简单替代方案。路由器应该比成熟的演员具有性能优势。

您可以使用参与者列表构建路由器,如此处所示。每次有变化时都这样做。(来源:akka 文档 - http://doc.akka.io/docs/akka/snapshot/scala/routing.html

val actor1 = system.actorOf(Props[ExampleActor1])
val actor2 = system.actorOf(Props[ExampleActor1])
val actor3 = system.actorOf(Props[ExampleActor1])
val routees = Vector[ActorRef](actor1, actor2, actor3)
val router2 = system.actorOf(Props().withRouter(
  RoundRobinRouter(routees = routees)))

这里显示的是 RoundRobin 路由器,但这与使用广播路由器没有任何不同。

重新创建它会更实用。

于 2013-05-21T17:05:03.630 回答