我学习Akka
,现在我想了解他们的基本Hello-world example(片段):了解阿卡你好世界的例子
public static void main(String[] args) throws TimeoutException {
// Create the 'helloakka' actor system
final ActorSystem system = ActorSystem.create("helloakka");
// Create the 'greeter' actor
final ActorRef greeter = system.actorOf(Props.create(Greeter.class), "greeter");
// Create the "actor-in-a-box"
final Inbox inbox = Inbox.create(system);
// Tell the 'greeter' to change its 'greeting' message
greeter.tell(new WhoToGreet("akka"), ActorRef.noSender()); //1 <------- Here
// Ask the 'greeter for the latest 'greeting'
// Reply should go to the "actor-in-a-box"
inbox.send(greeter, new Greet());
// Wait 5 seconds for the reply with the 'greeting' message
Greeting greeting1 = (Greeting) inbox.receive(Duration.create(5, TimeUnit.SECONDS));
System.out.println("Greeting: " + greeting1.message); //2 <-------- Here
// Change the greeting and ask for it again
greeter.tell(new WhoToGreet("typesafe"), ActorRef.noSender()); //3 <--- Here
inbox.send(greeter, new Greet());
Greeting greeting2 = (Greeting) inbox.receive(
Duration.create(5, TimeUnit.SECONDS)); //4 <-------------- Here
System.out.println("Greeting: " + greeting2.message);
//...
}
这是怎么回事了吗?
一,在//1
演员greeter
发消息忘记消息WhoToGreet("akka")
给无发件人。所以没有人会收到它。但由于某些不明的原因,我们在//2
从演员inbox
收到它(greeting1
)。为什么?这条消息如何与inbox
结束?我们没有发送到inbox
。这是一种奇迹......
二。在//3
几乎是相同的。我们告诉noSender,然后从inbox
recievin吧...
啊,如果我们调用'actorRef.tell(msg,anotherActorRef)'意味着由'anotherActorRef'引用的actor将'msg'发送给'actorRef',对吧? – Alupkers