?????? ??? ?????????????????????????·???????????
???????е??????????????????????????? Java 8 ???????
????????????????????????Щ?????????????????????? future ???????
????????????????????????????????????????????????????????????????С?
????????????????????????У????????????????????????????????÷??????????????????????????????????????????????? actor ????????????????? actor ???? Akka ??????Akka ?????????????????? JVM ??ó???????????????????????? ?ο???????????????????????????????
????Actor ????????
???????????????? actor ??????????? actor ???????????????Actor ??в?????????????? ????????Щ???????????? actor ??????????????????????????????????? actor???????????????????????????????????????????????????????????????? actor ????????????????????????????????????κ????????????? actor??
?????? Akka ?????У?actor ??????????????????????н????????????????????????????????Akka actor ?????????????????????????????????? Akka actor????????????????????????????????? actor ???á????????????y?????????·??? actor ??????????????? actor??Akka actor ??????????д??????????????????????? JVM ?????н??????????????????????? actor ???????
???????Щ?????????????????????Akka ??????????????????Щ??????????????????? actor ???????????????????????κν???????Щ??????????? actor ???????????????Actor ?????????????????????? 1 ???????????????? actor ????????????????????????????????????????????????Щ????????????????????? JVM ?????????δ?? Akka ?????У???????????????????ζ????????贛???????? actor ???????????????????????????й?????????????????????????????????
??????????
???????????????????? actor ???? Akka ?????????Щ?????????? hello ???????????????????????????????????????????????????????????嵥 1 ????? Scala ?е???? Akka ?汾??
?????嵥 1. ???? Scala hello
????import akka.actor._
????import akka.util._
????/** Simple hello from an actor in Scala. */
????object Hello1 extends App {
????val system = ActorSystem("actor-demo-scala")
????val hello = system.actorOf(Props[Hello])
????hello ! "Bob"
????Thread sleep 1000
????system shutdown
????class Hello extends Actor {
????def receive = {
????case name: String => println(s"Hello $name")
????}
????}
????}
?????嵥 1 ?е???????????????????Σ???????????? Hello1 ??ó???????С????????????? Akka ??ó???????????????
??????????? actor ????ActorSystem(...) ?У???
???????????????? actor??system.actorOf(...) ?У???????????? actor ??????? actor ???????
??????? actor ?????? actor ?????????hello !"Bob" ?У???
?????????????????? actor ????system shutdown ?У???
????system.actorOf(Props[Hello]) ????????? actor ??????????????????????????? Hello actor ????????????????????????? actor?????????С????????????????????????????????? Props ??????в???????????????? actor ?????????????????????? actor ??????????а????????б??????? Props ??????????????????β???????
????hello !"Bob" ??佫????????????????????? Bob?????????????? actor??! ??????? Akka ?б??????????????? actor ????????????????????????????????????????????????????????? tell() ????????????????
????????δ????? Hello actor ???壬?? class Hello extends Actor ????????????? actor ????????????????????????????? actor????????? receive???ú?????????????????????????receive ?????????????????????Щ?????????? — ??????У???? String ???????????ú??????????? actor ??????????????????????? String ????????????????????????
????Java ?е? Hello
?????嵥 2 ???????嵥 1 ?е? Akka Hello ????? Java ?е?????
?????嵥 2. Java ?е? Hello
????import akka.actor.*;
????public class Hello1 {
????public static void main(String[] args) {
????ActorSystem system = ActorSystem.create("actor-demo-java");
????ActorRef hello = system.actorOf(Props.create(Hello.class));
????hello.tell("Bob"?? ActorRef.noSender());
????try {
????Thread.sleep(1000);
????} catch (InterruptedException e) { /* ignore */ }
????system.shutdown();
????}
????private static class Hello extends UntypedActor {
????public void onReceive(Object message) throws Exception {
????if (message instanceof String) {
????System.out.println("Hello " + message);
????}
????}
????}
????}
?????嵥 3 ???????????? lambda ?? Java 8 ?? actor ???壬??? lambda ???? ReceiveBuilder ?????????????嵥 3 ???????????????嵥 2 ???С??
?????嵥 3. Java 8 ?? Akka Hello ?汾
????import akka.japi.pf.ReceiveBuilder;
????...
????private static class Hello extends AbstractActor {
????public Hello() {
????receive(ReceiveBuilder.
????match(String.class?? s -> { System.out.println("Hello " + s); }).
????build());
????}
????}
???????嵥 2 ?????嵥 3 ?е? Java 8 ???????????????????? —AbstractActor ???? UntypedActor— ?????????????????????????????????????ReceiveBuilder ??????????? lambda ?????????????????????????????????? Scala ??????????????????? Scala ?н??п?????????????????????????? Java Akka ?????????????????? Java 8 ????汾?????????Щ????????
???????????????
??????????ó???????У??????????? actor ?????????? Thread sleep 1000 ????????????????????????????????????????????????????????????????????????????? actor???? hello !"Bob" ????????????????У?
??????????????????“????”??Akka actor ???????е??????????? actor ????? actor λ??????? JVM ?У???? actor ????????????????С????????????????????????????? actor ???????С???????????????л??????????????????????????????? actor ?? receive ??????????????????????????????????????????????????????
?????????????????
????“???????????” ???????????????????????????????Akka ??? actor ???????????λ??????????ζ?????????????κ???????????????????? actor ??λ???? JVM ?У??????????????????????С???????????????????????????????????????????
????“Akka ??????????????????????????????????????????????? Akka ???????????? ”
???????????????????й??Akka ???????????????????????????????????????????????ó?????????????????????????????????????????????? Akka ??????????????????????????????????????????????????????????????????????????????????????Щ???????????????????????У?????ó???????????漰????????????????????ó????????????д????????????????????壬?????????????????????
????Akka ???? ????????????Σ??????????????????????? actor ????????????? actor ???????????????????????????? actor ?????????????????? actor A ?????????? actor B????Щ??????????????????? actor A ?????????? actor C???????????????????? actor B ??????????? actor C?????罫???? A ?????????? C????B ?????????????? A ????????????????????
?????? ?嵥 1 ??????У????????????????????????????? JVM ?????У???????????????????????????????????????????????????? Akka ??п?????洢?????????????б??÷????????????????????????嵥 1 ????????δ?????????????????κμ??裬???????? actor ?????????????
????Actor ????
????Akka ?? actor ??????????????????? actor??????????????????? actor???? Hello1 ????????????????Щ actor ?????Ч????????á???????????????????? actor ??????
?????嵥 1 ???????????????????????actor ????? — ?????????????????????? actor???λ?????????????????仰???????嵥 4 ?? actor ??????Щ?????????????????????
?????嵥 4. Polyglot Scala hello
????object Hello2 extends App {
????case class Greeting(greet: String)
????case class Greet(name: String)
????val system = ActorSystem("actor-demo-scala")
????val hello = system.actorOf(Props[Hello]?? "hello")
????hello ! Greeting("Hello")
????hello ! Greet("Bob")
????hello ! Greet("Alice")
????hello ! Greeting("Hola")
????hello ! Greet("Alice")
????hello ! Greet("Bob")
????Thread sleep 1000
????system shutdown
????class Hello extends Actor {
????var greeting = ""
????def receive = {
????case Greeting(greet) => greeting = greet
????case Greet(name) => println(s"$greeting $name")
????}
????}
????}
?????嵥 4 ?е? actor ?????δ?????????????????????Щ??????嵥???????????壺Greeting ????? Greet ?????????????????????????????????? Hello actor ??? Greeting ????????????????????????? greeting ?????? Greet ?????????????? greeting ??? Greet ???????????????γ??????????????????д???ó???????????????????????д????????????????????????????????????? actor ?????????????????
????Hello Bob
????Hello Alice
????Hola Alice
????Hola Bob
?????嵥 4 ?в?????????′???????????????? Java ?汾??????????????????????????????μ? ?ο???????????com.sosnoski.concur.article5java.Hello2 ?? com.sosnoski.concur.article5java8.Hello2??
????????????
?????????? actor ????????? actor ???????????????????????????н??????????????????Щ actor ??????????????????????????????嵥 5 ???? Hello ?????????????????????? actor ???ú??????
?????嵥 5. Actor ????????
????object Hello3 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
????case class TellName(name: String)
????def props(name: String?? greeting: String) = Props(new Greeter(name?? greeting))
????}
????class Greeter(myName: String?? greeting: String) extends Actor {
????import Greeter._
????def receive = {
????case Greet(peer) => peer ! AskName
????case AskName => sender ! TellName(myName)
????case TellName(name) => println(s"$greeting?? $name")
????}
????}
????}
?????嵥 5 ????????а?????????μ? actor???? Greeter actor??Greeter ?? Hello2 ??????????????????????????
????????????????????????? Greeter ???
???????????????????????? Scala ?????????????? Java ???????????????????????????? actor ????????? helper ??
?????? Greeter actor ?????????????
??????????????????????
????Howya doing?? Alice
????Happy to meet you?? Bob