??????????????и?????Σ??????????Щ?е????????????????????? Akka actor ???????????????????????д?????????????????????????????????? “?????????????” ???????????????????
????Java ?е? Greeter
?????嵥 6 ??????嵥 5 ?е? Akka Greeter ???????? Java ?汾??
?????嵥 6. Java ?е? Greeter
????public class Hello3 {
????public static void main(String[] args) {
????ActorSystem system = ActorSystem.create("actor-demo-java");
????ActorRef bob = system.actorOf(Greeter.props("Bob"?? "Howya doing"));
????ActorRef alice = system.actorOf(Greeter.props("Alice"?? "Happy to meet you"));
????bob.tell(new Greet(alice)?? ActorRef.noSender());
????alice.tell(new Greet(bob)?? ActorRef.noSender());
????try {
????Thread.sleep(1000);
????} catch (InterruptedException e) { /* ignore */ }
????system.shutdown();
????}
????// messages
????private static class Greet {
????public final ActorRef target;
????public Greet(ActorRef actor) {
????target = actor;
????}
????}
????private static Object AskName = new Object();
????private static class TellName {
????public final String name;
????public TellName(String name) {
????this.name = name;
????}
????}
????// actor implementation
????private static class Greeter extends UntypedActor {
????private final String myName;
????private final String greeting;
????Greeter(String name?? String greeting) {
????myName = name;
????this.greeting = greeting;
????}
????public static Props props(String name?? String greeting) {
????return Props.create(Greeter.class?? name?? greeting);
????}
????public void onReceive(Object message) throws Exception {
????if (message instanceof Greet) {
????((Greet)message).target.tell(AskName?? self());
????} else if (message == AskName) {
????sender().tell(new TellName(myName)?? self());
????} else if (message instanceof TellName) {
????System.out.println(greeting + "?? " + ((TellName)message).name);
????}
????}
????}
????}
?????嵥 7 ???????? lambda ?? Java 8 ?汾?????????汾?????????????????????????????????涼????????
?????嵥 7. Java 8 ?汾
????import akka.japi.pf.ReceiveBuilder;
????...
????private static class Greeter extends AbstractActor {
????private final String myName;
????private final String greeting;
????Greeter(String name?? String greeting) {
????myName = name;
????this.greeting = greeting;
????receive(ReceiveBuilder.
????match(Greet.class?? g -> { g.target.tell(AskName?? self()); }).
????matchEquals(AskName?? a -> { sender().tell(new TellName(myName)?? self()); }).
????match(TellName.class?? t -> { System.out.println(greeting + "?? " + t.name); }).
????build());
????}
????public static Props props(String name?? String greeting) {
????return Props.create(Greeter.class?? name?? greeting);
????}
????}
????????????
????Akka ??? Props ???????????????????? actor????? Props ?????? actor ???????????????????????????????????????á???????????????????????? Props ?????????嵥 5 ?е?????? actor ???????????????????? (pass-by-name) ????????? Props ????????????????????????ù?????????????????????????????????????? Java ???????????????????????????
?????? actor ???????? Props ????????????????????? actor ???????????????????? actor ??????????????????????????? ?嵥 5 ?е????????????????? Props(classOf[Greeter]?? name?? greeting)??
???????????????????? Props ??????????????? actor ?????????????л?????????????????罫 Props ??????????и? actor ??????κε???????????????????????????????? ?嵥 5 ?е??÷???????????÷???? JVM ????????л??????????
?????? Scala ?????д??? Props ????? Akka ????????????????????????ж??幤?????????? ?嵥 5 ???????????????? Props ??????????????????÷?????????????????κ????????????? actor ????? this ???á?????????????? actor ???????????????????????????????й??????????λ????λ?á????? Java actor??????? actor ??????t???????????????? ?嵥 6 ????????????
????????????? Actor
?????嵥 5 ?е???? Greeter actor ???????????????????????????????????????? actor ???????????????? actor ???????Greeteractor ?????????? actor ??????????????????????????AskName ?????AskName ???????????κ??????????????? Greeter ????????????????? TellName ?????????? TellName ?????????????????? Greeter ?????????? TellName ???????????????????????
????????? actor ???????????????? Akka ?????Щ????????????????????????? ActorRef???????????????????е??κ?????????????? actor ?????????? sender() ????????????Щ????????????Greeter actor ????? AskName ?????????л???÷???????????? TellName ????????????? actor??
????Akka ??????????????? actor ??????????????????????????????????????????? actor ??????? actor ???????????????? actor ???о????????????????????????????????-??????????????????????????????????????????????????? actor ??????????actor ??????ó????????????????????????????? deadletter actor ?????? Akka ???????????κ????????????????? actor ????????? deadletter actor???????????????????????? actor ??????????????????????????????н???????????????????????
???????? actor ??????
??????????????????????????????????κ???????????????????????????? Greeter ?????Akka actor ???佻??????????????????????????????????????? actor ?? ActorRef ???????????
??????д??????? actor ????????????????????? ???? actor ??????????????????????????????????????????????? Akka ?У?actor ??????????????????????????????????????????????????????????????? actor ????????????????????????? actor ??????????????? actor ?????????д????κ???????????
???????? Akka ??????????? actor????????????????????????????????????? actor ??? actor ???????á????????????????? actor ??????y???? actor ????????? actor ?????????????????????????????????????????????????????鷳????????????????????????? actor ???????????????? actor ???????????????κ??????ó????п?????????? actor ????????????????????????????????
?????????????
????Akka ??????????????????? actor ??乲?????????????????????????????????? — ?????????????????????????????????????????????μ?????????????????????????????????????????????actor ???????????????С?????? actor ??乲???????????????????? actor ???????????Э??????????????????????????????????????????????????????????????????????????????з????????????????????? actor ???????????????????????
????????????????????????????????????????????????????????а????κζ?????Щ??????????????????????????????????????????ж???Akka ?????????????????? Akka ????????????????????????????????Щ??????????????????????δ???? Akka ?汾?????????????????????????????
????????????
?????嵥 5 ?е????????? tell ????????????????? Akka ?У??????? ask ?????????????????????ask ???????? ? ??????????ask ???????????????????? Future ????????????????嵥 8 ?У???????????嵥 5 ?е??????? ask ?????? tell??
?????嵥 8. ??? ask
????import scala.concurrent.duration._
????import akka.actor._
????import akka.util._
????import akka.pattern.ask
????object Hello4 extends App {
????import Greeter._
????val system = ActorSystem("actor-demo-scala")
????val bob = system.actorOf(props("Bob"?? "Howya doing"))
????val alice = system.actorOf(props("Alice"?? "Happy to meet you"))
????bob ! Greet(alice)
????alice ! Greet(bob)
????Thread sleep 1000
????system shutdown
????object Greeter {
????case class Greet(peer: ActorRef)
????case object AskName
????def props(name: String?? greeting: String) = Props(new Greeter(name?? greeting))
????}
????class Greeter(myName: String?? greeting: String) extends Actor {
????import Greeter._
????import system.dispatcher
????implicit val timeout = Timeout(5 seconds)
????def receive = {
????case Greet(peer) => {
????val futureName = peer ? AskName
????futureName.foreach { name => println(s"$greeting?? $name") }
????}
????case AskName => sender ! myName
????}
????}
????}
???????嵥 8 ??????У?TellName ???????滻? ask??ask ????????? future ??????? Future[Any]??????????????????????????????? future ??????foreach ??? import system.dispatcher ???????????????????????? println????? future δ?????????????????????????????????ж???? 5 ??????????????????????????????????
?????????ask ??????????????????? actor ??????????г??н顣???н???????? Promise ??????????????????? actor ???á??????????????????????????????????????????????г?????????? actor ?????? future??
??????? ask ???????Щ?????????????????????? actor ?????????????????????????????δ?? future ????????е?????????????? actor ???κο???????????????У???? actor ???????????? tell ?????????????ask ????????????????????ó??????????? actor????????????????????????????????? actor ?????????? actor ??????????
????С???
????“???????????????????????????????????????????????????μ? actor??”
????ask ??????????????? actor ??????? Akka ?????????????????????????????????? actor ????????м????????????????????????????????? actor ????е???????????????????????????????????????????????????????????????????????????????? actor ????????????????????н????????????????????ε??????????????? ask ???????????????? actor??
????Akka actor ??????????? actor ?????? 300 ?? 400 ???????? actor ?????????洢?????????????????????????????????????????????? actor????????? actor ????????????????????????????д???????????д?????????????????????????????????????????????????????????????????????μ? actor??
???????伸??
????Akka ??????????????? Akka ?? actor ?????????????????????????????????????????????????е????е???????????????????????????????????????? actor ????У???????????????????????????????????????????????????????????actor ????????????????????????????????????ó????????????????????????л???ν????????
???????????????????????????? Akka??????????????????????????????????????????????? actor ???? actor ????????????????????????и? actor ?????????