Akka Actor Inbox_信箱
Inbox_send_receive
Inbox 形象的表示为Actor的信箱,具有收发信息的功能,所以,Inbox 对象有这两种方法:
send(target, "hello") ;
receive(Duration. create(1, TimeUnit. SECONDS) )
以下Inbox 使用示例,首先定义一个Actor,
package com.usoft3;
import akka.actor.UntypedActor;
/**
* Created by liyanxin on 2015/1/12.
*/
public class MyActor extends UntypedActor {
private int x;
private int y;
public MyActor(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void onReceive(Object message) throws Exception {
System.out.println("message=" + message);
int result = x + y;
this.getSender().tell(result, this.getSelf());
this.getContext().stop(this.getSelf());
}
}
下面是用Inbox 和上边的 Actor 进行交互,如下,
package com.usoft3;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Inbox;
import akka.actor.Props;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
import java.util.concurrent.TimeUnit;
/**
* Created by liyanxin on 2015/1/12.
*/
public class InboxDemo {
public static void main(String args[]) {
ActorSystem system = ActorSystem.create("myActorSystem");
Inbox inbox = Inbox.create(system);
ActorRef cal = system.actorOf(Props.create(MyActor.class, 5, 7), "myActor");
inbox.send(cal, "hello world!");
Integer result = (Integer) inbox.receive((FiniteDuration) Duration.create(1, TimeUnit.SECONDS));
System.out.println("result=" + result);
}
}
Inbox_watch
使用Inbox 监视一个Actor的关闭
如下定义一个Actor,
package com.usoft3;
import akka.actor.UntypedActor;
/**
* Created by liyanxin on 2015/1/12.
*/
public class MyActor extends UntypedActor {
private int x;
private int y;
public MyActor(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void onReceive(Object message) throws Exception {
System.out.println("message=" + message);
int result = x + y;
this.getSender().tell(result, this.getSelf());
this.getContext().stop(this.getSelf());
}
}
使用Inbox 监视该Actor,
package com.usoft3;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Inbox;
import akka.actor.Props;
import akka.actor.Terminated;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
import java.util.concurrent.TimeUnit;
/**
* Created by liyanxin on 2015/1/12.
*/
public class InboxWatchDemo {
public static void main(String args[]) {
ActorSystem system = ActorSystem.create("myActorSystem");
Inbox inbox = Inbox.create(system);
ActorRef cal = system.actorOf(Props.create(MyActor.class, 5, 7), "myActor");
inbox.watch(cal);
cal.tell("good afternoon", ActorRef.noSender()); //这个消息没有发送者
Terminated terminated = (Terminated) inbox.receive((FiniteDuration) Duration.create(1, TimeUnit.SECONDS));
System.out.println(terminated.toString());
System.out.println(terminated.getActor().path());//actor path = akka://myActorSystem/user/myActor
}
}
运行结果如下,
message=good afternoon
[INFO] [01/12/2015 15:57:12.442] [myActorSystem-akka.actor.default-dispatcher-3] [akka://myActorSystem/deadLetters] Message [java.lang.Integer] from Actor[akka://myActorSystem/user/myActor#-2054229435] to Actor[akka://myActorSystem/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
Terminated(Actor[akka://myActorSystem/user/myActor#-2054229435])
akka://myActorSystem/user/myActor
===================END===================