我有两个不同的ActorSystem类及其相应的actor.class1中的actor如何向class2中的actor发送消息?
为什么你有2 ActorSystem秒?除非你有充分的理由,否则你应该创造所有的演员ActorSystem.创建一个ActorSystem非常昂贵的通信以及错误处理更难.这是演员之间沟通的一个简单例子:
class Foo extends Actor {
val barActor = context.actorFor("/user/bar")
def receive = {
case 'Send => barActor ! "message from foo!"
}
}
class Bar extends Actor {
def receive = {
case x => println("Got " + x)
}
}
object Main {
def main(args: Array[String]) {
val system = ActorSystem("MySystem")
val foo = system.actorOf(Props[Foo], "foo")
val bar = system.actorOf(Props[Bar], "bar")
foo ! 'Send
}
}
Run Code Online (Sandbox Code Playgroud)
使用system.actorFor或context.actorFor,您可以检索ActorRef给定路径.用户创建的actor的路径始终以/user并包括所有父actor.因此,如果你有3个演员的层次结构,那么路径就可以了/user/actorA/actorB/actorC.