Example
#full-example import Akka.actor. {Actor, Actorref, Actorsystem, Props}//#greeter-companion//#greeter-messages object Greeter {//#greeter-messages D
EF Props (message:string, printeractor:actorref): props = props (new Greeter (message, Printeractor))//#greeter-messages Final Case Class Whotogreet (who:string) Case Object Greet}//#greeter-messages//#greeter-companion//#greeter-act or class Greeter (message:string, printeractor:actorref) extends Actor {import greeter._ import printer._ var gre Eting = "" def receive = {case Whotogreet (who) => greeting = S "$message, $who" Case Greet = >//#greeter-send-message Printeractor! Greeting (greeting)//#greeter-send-message}}//#greeter-actor//#printer-companion//#printer-messages Object P Rinter {//#printer-messages def props:props = Props[printer]//#printer-messages Final Case class greeting (greet
ing:string)}//#printer-messages//#printer-companion
#printer-actor class Printer extends actor {Import Printer._ def receive = {case greeting (greeting) => println (S "Greeting received (from ${sender}): $greeting")}//#printer-actor//#main-class Object Akkaquickstart Extends App {import greeter._//Create the ' Helloakka ' actor system val System:actorsystem = Actorsystem ("Helloa Kka ")//#create-actors//Create the printer actor Val printer:actorref = System.actorof (Printer.props," Printerac Tor ")//Create the ' greeter ' actors val howdygreeter:actorref = System.actorof (Greeter.props (" Howdy ", printer), "Howdygreeter") val hellogreeter:actorref = System.actorof (Greeter.props ("Hello", printer), "Hellogreeter") Val
Gooddaygreeter:actorref = System.actorof (Greeter.props ("Good Day", printer), "Gooddaygreeter")//#create-actors #main-send-messages Howdygreeter! Whotogreet ("Akka") Howdygreeter! Greet Howdygreeter! Whotogreet ("Lightbend") Howdygreeter!
Greet
Hellogreeter! Whotogreet ("Scala") Hellogreeter! Greet Gooddaygreeter! Whotogreet ("Play") Gooddaygreeter!
Greet//#main-send-messages}//#main-class//#full-example
parsing
The 1.props method returns the props instance, which is used to create the actor
2.
Class Greeter (message:string, printeractor:actorref) extends Actor {
import greeter._
import printer._
var greeting = ""
def receive = {case
whotogreet (WHO) =>
greeting = S "$message, $who" Case
Greet = >
printeractor! Greeting (greeting)
}
}
Whotogreet Modify the state of actor itself, greet send greeting sent to Printeractor
3.ActorRef is a reference to actor
4.akka.actor.actorsystem is actor to create a factory
Create printer actor
Val printer:actorref = system.actorof (Printer.props, "Printeractor")
5.ACTORREF send messages with! Actor Send Message is asynchronous. A actor doesn't do anything until he receives the message.
Howdygreeter! Whotogreet ("Akka")