RoundRobinRouter和RoundRobinRoutinglogic之间的区别

时间:2016-08-09 09:37:51

标签: scala akka

所以我正在阅读有关akka的教程,并且遇到了这个http://manuel.bernhardt.io/2014/04/23/a-handful-akka-techniques/,我认为他解释得非常好,我最近刚拿起scala,并且在上面的教程中遇到了困难,

我想知道RoundRobinRouter和当前的RoundRobinRouterLogic有什么区别?显然,实施方式完全不同。

以前RoundRobinRouter的实现是

val workers = context.actorOf(Props[ItemProcessingWorker].withRouter(RoundRobinRouter(100)))

with processBatch

 def processBatch(batch: List[BatchItem]) = {

    if (batch.isEmpty) {
      log.info(s"Done migrating all items for data set $dataSetId. $totalItems processed items, we had ${allProcessingErrors.size} errors in total")
    } else {
      // reset processing state for the current batch
      currentBatchSize = batch.size
      allProcessedItemsCount = currentProcessedItemsCount + allProcessedItemsCount
      currentProcessedItemsCount = 0
      allProcessingErrors = currentProcessingErrors ::: allProcessingErrors
      currentProcessingErrors = List.empty

      // distribute the work
      batch foreach { item =>
        workers ! item
      }
    }

  }

这是我对RoundRobinRouterLogic的实现

  var mappings : Option[ActorRef] = None

  var router = {
    val routees = Vector.fill(100) {
      mappings = Some(context.actorOf(Props[Application3]))
      context watch mappings.get
      ActorRefRoutee(mappings.get)
    }
    Router(RoundRobinRoutingLogic(), routees)
  }

并将processBatch视为

 def processBatch(batch: List[BatchItem]) = {

    if (batch.isEmpty) {
      println(s"Done migrating all items for data set $dataSetId. $totalItems processed items, we had ${allProcessingErrors.size} errors in total")
    } else {
      // reset processing state for the current batch
      currentBatchSize = batch.size
      allProcessedItemsCount = currentProcessedItemsCount + allProcessedItemsCount
      currentProcessedItemsCount = 0
      allProcessingErrors = currentProcessingErrors ::: allProcessingErrors
      currentProcessingErrors = List.empty

      // distribute the work
      batch foreach { item => 
        // println(item.id)            
        mappings.get ! item
      }
    }
  }

我无论如何都无法运行本教程,而且它正处于迭代批处理列表的位置。我想知道我做错了什么。

由于

2 个答案:

答案 0 :(得分:1)

首先,你必须区分它们之间的差异。

RoundRobinRouter是一个使用循环来选择连接的路由器。

虽然 RoundRobinRoutingLogic使用循环法选择一个空白

您可以提供自己的RoutingLogic(它帮助我了解Akka如何在幕后工作)

class RedundancyRoutingLogic(nbrCopies: Int) extends RoutingLogic {
  val roundRobin = RoundRobinRoutingLogic()
  def select(message: Any, routees: immutable.IndexedSeq[Routee]): Routee = {
    val targets = (1 to nbrCopies).map(_ => roundRobin.select(message, routees))
    SeveralRoutees(targets)
  }
}
文档http://doc.akka.io/docs/akka/2.3.3/scala/routing.html上的

链接 附:这个文档很清楚,它帮助了我最多

答案 1 :(得分:1)

实际上我误解了这个方法,并发现解决方案是使用http://doc.akka.io/docs/akka/2.3-M2/project/migration-guide-2.2.x-2.3.x.html中所述的RoundRobinPool

  

例如,RoundRobinRouter已重命名为RoundRobinPool或   RoundRobinGroup取决于您实际使用的类型。

来自

val workers = context.actorOf(Props[ItemProcessingWorker].withRouter(RoundRobinRouter(100)))

val workers = context.actorOf(RoundRobinPool(100).props(Props[ItemProcessingWorker]), "router2")