gpt4 book ai didi

java - 如何在scala akka中找到父 Actor

转载 作者:行者123 更新时间:2023-11-30 06:57:44 24 4
gpt4 key购买 nike

我正在使用 scala akka actor 模型。我有一个父 Actor 创建了 n 个子 Actor 。小 Actor 先互相交谈,然后将答案报告给大 Actor 。但我无法让它适用于报告部分。代码结构如下:

class Master(n:Int) extends Actor{
val system =ActorSystem("mysystem")
for(i <- 1 to n){
val child=system.actorOf(Props(new Node),name=i.toString)
}
... code let child actor talk with each other ...
def receive={
case _=>"received"
}
}
class Node extends Actor{
def receive={
case => ... some code talking with each other...
var master=context.actorSelection("../Master")
master ! "talk back to master"
}
}
def main() {
val Master=system.actorOf(Props(new Master(10)),name="Master")
}

最佳答案

让主节点将其自引用传递给节点。相关更改的行是:

... // In Master:
val child=context.actorOf(Props(new Node(self)),name=i.toString)
...
... // Definition of Node:
class Node(master: ActorRef) extends Actor{
...
//var master=context.actorSelection("../Master") - this line no longer required.
... //etc.

关于java - 如何在scala akka中找到父 Actor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33327411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com