如何使用参数化actor在akka中创建路由器? [英] How to create routers in akka with parameterized actors?

查看:913
本文介绍了如何使用参数化actor在akka中创建路由器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Scala中使用广播路由器,如果我不误认为它应该看起来像这样:

I am trying to use a broadcast router in Scala, if I'm not mistaken it should look like this:

val system = ActorSystem("My beautiful system")
val workerRouter = system.actorOf(Props[Agent].withRouter(BroadcastRouter(individualDefinitions.size)), name = "agentRouter")

这是我从我正在关注的教程

workerRouter充当另一个actor,我可以发送消息到这个路由器,将它们发送给所有的代理(多达个人定义我有)。

The workerRouter acts as another actor and I can send messages to this router that will send them to all the Agents (as many as individualDefinitions I have).

问题是,我想使用单独的定义来构建代理,他们实际上在构造函数中使用一些参数,这些参数在个人定义中。

The problem is that I'd like to use the individual definitions to build the agents, they actually take some parameters in the constructor and those parameters are in the individualDefinitions.

问:如何告诉路由器

请注意,每个actor应该得到一个单独的定义,它们都是不同的。我不能使用解决方案在一个相关的问题,其中构造函数接收常量:在Akka Java actor模型中,路由器是否可以创建具有非默认构造函数的actors?

Please note each actor should get one individualDefinition and they are all different. I cannot use the solution in a related question where the constructor receives constants: In Akka Java actor model, can a router create actors with non-default constructor?

请注意,这里每个actor应该有不同的参数,如果它们中的一个被重新启动,它应该获得与它首先相同的参数。我不知道该解决方案是否可以修改。

Please note that here each actor should have different parameters, if one of them is restarted it should get the same parameters it got in the first place. I don't know if that solution could be modified to do that.

一个可能的解决方案可能是使用actor作为路由器,分离创建(构造函数)和路由,如问题 Akka(java),非阻止广播给所有儿童

A possible solution could be using an actor as the router, to separate creation (constructor) and routing, as in question Akka (java), non blocking broadcast to all children.

我不确定在这种情况下是否是正确的方法。使用actor作为路由器有几个问题(除了优雅之外)。我担心的作用,作为一个路由器正在重新启动,失去所有的订阅者。如果演员在半场重新开始,一些演员也可能错过一些消息,如果我没有错误。

I'm not sure that is the "right" approach in this case. Using an actor as the router has several problems (besides of elegance). I am concerned about the actor that works as a router being restarted and losing all its subscribers. If the actor is restarted in half of a loop some actors could also miss some messages if I'm not mistaken.

谢谢。

以下示例将创建2个不同创建的actors,然后创建一个循环路由器,将路由消息路由到它们。

The following example will create 2 actors created differently and then create a round robin router which will route the messages to them.

class MyActor(param1: String) extends Actor with ActorLogging {
  def receive: Actor.Receive = {
    case msg => log.info("Message from {}: {}", param1, msg)
  }
}

object MyActor {
  def apply(param: String): Props = Props(new MyActor(param))
}

object Main extends App {
  val system = ActorSystem()

  val a1 = system.actorOf(MyActor("actor1"))
  val a2 = system.actorOf(MyActor("actor2"))

  val routerProps = Props.empty.withRouter(RoundRobinRouter(routees = Vector(a1, a2)))

  val router = system.actorOf(routerProps)

  for (i <- 1 to 10) {
    router ! i
  }

  readLine()
  system.shutdown()
}

有关详情,请参阅: http:// doc。 akka.io/docs/akka/2.2.0/scala/routing.html

这篇关于如何使用参数化actor在akka中创建路由器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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