Getting started with Scala Akka actor (1) __scala

Source: Internet
Author: User
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")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.