Official Document: Http://developer.lightbend.com/guides/akka-quickstart-java/?_ga= 2.177525157.1012573474.1504767665-546319647.1504766934
I. AKKA Hello World Example:
1. First you need to create a actorsystem, which is the container for all actors to run.
2. Next create the Greeter actor and the printer actor.
3. Send the message to the Greeter actor instance, which stores the message internally first.
4. The instruction message in the last greeter actor triggers them to send a message to the printer actor, and the printer actor performs the corresponding action.
Two. Define actors and messages
Messages can be any type (any subtype of Object).
When defining actors and their messages, observe the following specifications:
1. Because messages is the actor's public API, defining a name for messages is rich in semantics and domain-specific meaning.
2.messages is immutable because they are shared between different threads.
3. Put the actor-related messages as a static inner class into the actor class, which makes it easier to understand what type of message the actor needs to handle.
4. In the actor class, use the static method props to describe how to construct the actor.
1 Packagecom.lightbend.akka.sample;2 3 ImportAkka.actor.AbstractActor;4 ImportAkka.actor.ActorRef;5 ImportAkka.actor.Props;6 Importcom.lightbend.akka.sample.Printer.Greeting;7 8 Public classGreeterextendsAbstractactor {9 Static PublicProps Props (String message, Actorref printeractor) {Ten returnProps.create (Greeter.class, ()NewGreeter (message, printeractor)); One } A - Static Public classWhotogreet { - Public FinalString who; the - PublicWhotogreet (String who) { - This. who =Who ; - } + } - + Static Public classGreet { A PublicGreet () { at } - } - - Private FinalString message; - Private Finalactorref Printeractor; - PrivateString greeting = ""; in - PublicGreeter (String message, Actorref printeractor) { to This. Message =message; + This. Printeractor =Printeractor; - } the * @Override $ PublicReceive createreceive () {Panax Notoginseng returnReceivebuilder () -. Match (Whotogreet.class, WTG { the This. Greeting = message + "," +wtg.who; + }) A. Match (Greet.class, X- { thePrinteractor.tell (Newgreeting (greeting), getself ()); + }) - . Build (); $ } $}
1 Packagecom.lightbend.akka.sample;2 3 ImportAkka.actor.AbstractActor;4 ImportAkka.actor.ActorRef;5 ImportAkka.actor.Props;6 Importakka.event.Logging;7 ImportAkka.event.LoggingAdapter;8 9 Public classPrinterextendsAbstractactor {Ten Static PublicProps Props () { One returnProps.create (Printer.class, ()NewPrinter ()); A } - - Static Public classGreeting { the Public FinalString message; - - Publicgreeting (String message) { - This. Message =message; + } - } + A PrivateLoggingadapter log = Logging.getlogger (GetContext (). Getsystem (), This); at - PublicPrinter () { - } - - @Override - PublicReceive createreceive () { in returnReceivebuilder () -. Match (greeting.class, greeting { to Log.info (greeting.message); + }) - . Build (); the } *}
Three. Create an Actor object
The actor cannot be created using new, but instead uses the factory to create the actor object, instead of the actor instance, the factory returns a reference Akka.actor.ActorRef, which points to the actor instance.
Akka.actor.ActorSystem is similar to spring's beanfactory. It acts as a container for actors to manage the actor's life cycle. Factory Method Actorof creates an actor object with two parameters, one parameter is the configuration object props, and one parameter is the actor's name.
1 Final 2 System.actorof (Printer.props (), "Printeractor"); 3 Final 4 System.actorof (Greeter.props ("Howdy", Printeractor), "Howdygreeter"); 5 Final 6 System.actorof (Greeter.props ("Hello", Printeractor), "Hellogreeter"); 7 Final 8 System.actorof (Greeter.props ("Good Day", Printeractor), "Gooddaygreeter");
Four. Asynchronous communication
Actors are responsive and message-driven. An actor doesn't do anything to know that it received a message. Actors communicates through asynchronous messages. This ensures that the sender of the message (sender) does not have to stop waiting for their message to be processed by the recipient (recipient). Instead, the sender will be able to do something else just by putting the message in the recipient's mailbox (mailbox). And this mailbox is an orderly message queue on the book. Multiple messages from the same actor are ordered, and messages from different actors are interleaved.
When the actor does not process the message, it is in a paused state, at which point it does not consume any resources other than memory.
Five. Send message to actor
Put the message in the actor's mailbox (mailbox), you need to call Actorref's Tell method. Give me a chestnut:
To send the message to the greeter Actor:
1Howdygreeter.tell (NewWhotogreet ("Akka"), Actorref.nosender ());2Howdygreeter.tell (NewGreet (), Actorref.nosender ());3 4Howdygreeter.tell (NewWhotogreet ("Lightbend"), Actorref.nosender ());5Howdygreeter.tell (NewGreet (), Actorref.nosender ());6 7Hellogreeter.tell (NewWhotogreet ("Java"), Actorref.nosender ());8Hellogreeter.tell (NewGreet (), Actorref.nosender ());9 TenGooddaygreeter.tell (NewWhotogreet ("Play"), Actorref.nosender ()); OneGooddaygreeter.tell (NewGreet (), Actorref.nosender ());
The Greeter actor sends a message to the printer actor:
Printeractor.tell (new greeting (greeting), getself ());
Akka Quickstart with java-notes