我學習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