【问题标题】:Actor not stopped in `akka aggregator pattern` documentationActor 未在“akka 聚合器模式”文档中停止
【发布时间】:2014-02-23 16:21:25
【问题描述】:

我阅读了the aggregator pattern http://doc.akka.io/docs/akka/2.2.3/contrib/aggregator.html 并在sample use case 段落中找到了一些东西

def fetchSavingsAccountsBalance() {
      context.actorOf(Props[SavingsAccountProxy]) ! GetAccountBalances(id)
      expectOnce {
        case SavingsAccountBalances(balances) ⇒
          results += (Savings -> balances)
          collectBalances()
      }
    }

发现context.actorOf创建的actor一直没有停过,应该是这样吗?

【问题讨论】:

    标签: scala akka


    【解决方案1】:

    我相信阻止聚合器参与者是您自己的责任。来自文档:

    The aggregator should terminate after the response is sent (or timed out). 
    

    我将其解释为停止是您自己的责任,而不是框架为您所做的事情。我猜如果它确实自己停止了,文档会说以下内容:

    The aggregator will terminate...
    

    您还会注意到,在文档中的多个示例中明确调用了context.stop。此外,您提供的示例是链式聚合示例的一部分。它会收集一堆单独的数据(链接在一起),然后最终响应所有聚合数据并用这段代码明确地停止自己。

    def processFinal(eval: List[Int]) {
      // Select only the entries coming back from eval
      originalSender ! FinalResponse(eval map values)
      context.stop(self)
    }
    

    【讨论】:

    • 你是对的,collectBalances 方法停止了聚合器
    【解决方案2】:

    默认情况下,Actor 不会自行关闭,除非您:

    • 停止actor系统context.system.shutdown()
    • 停止父actor或
    • 使用context.stop(self) 明确停止该演员。

    由于您使用聚合器模式并且只需要收集一次结果,您就可以在处理完成后关闭整个参与者系统。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 2019-07-07
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多