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
に送っていませんでした。それは奇跡のようなものです...
II。 //3
では、ほぼ同じことが起こっています。それは認めることinbox.receive
を使用する前に、
// Ask the 'greeter for the latest 'greeting'
// Reply should go to the "actor-in-a-box"
inbox.send(greeter, new Greet());
と呼ばれている:私たちは、これはgreeter
アクターに与えられた挨拶を消費するように受信トレイを告げる行さ
ああ、 'actorRef.tell(msg、anotherActorRef) 'と呼ばれると、' anotherActorRef'によって参照されたアクターが 'msg'を' actorRef'に送ります。 – Alupkers