Scala has been using Akka as a concurrency model since 2.10, and this article is the first example of Akka.
1.Akka Getting Started instance
PackageCom.tv189.actorImportAkka.actor. {Actor, Actorsystem, Props}/*** Created by Molyeo on 2015/8/6.*/Object AkkaTest01extendsApp {val system= Actorsystem ("Akkatest") Val Helloactor= System.actorof (Props[helloactor], name = "Helloactor") Helloactor! "Hello"Helloactor! "Yes"classHelloactorextendsActor {def receive= { Case"Hello" and "= println" ("Hello Back to You") Case_ = = println ("Huh?")) } }}
The results of the operation are as follows:
Hello back to you
Huh?
- First create the Helloactor by inheriting the actor, and override the Receive method
- Define Actorsystem
- The specific actor instance is obtained by Actorof method Helloactor
- Call! Method Send Message
Key points of the 2.Actor model 2.1.Actor model
- Actor is the smallest abstract unit in actor-based systems, as object is to OOP
- An actor encapsulates the state and behavior
- Outside the actor to get its state, field, execution method, and actor's interaction can only be through the message
- An actor has a mailbox mailbox that sends messages sent externally to the queue
- Actor's lifetime is waiting for MSG, and then take out the message in mailbox to process
The organization view of 2.2.Actor model
- The actor system as a company, from top to bottom have a strict hierarchical relationship, the company employees are person, is also actor
- One actor has only one supervisor called supervisor, the actor who is the actor of Create.
- An actor may have more than one subordinate soldier, a real dirty-tired actor.
- An actor may have multiple siblings of sibling actor
2.3. Key to developing the actor system
- Entrusted entrusted entrusted!!! To do more!
- The chief of the company can not bear all the responsibility, his main job is to assign company work and supervision
- The heads of departments receive tasks, either by themselves or by subdividing tasks and then assigning them to their subordinates and supervising
- If the company has more layers, continue the dirty work of the supervisor.
- Bottom-level employees receive fine-grained tasks, doing what they're best at.
2.4.Actor of failure handling
- No perfect, no actor. 100% Complete Mission
- If a task fails, an actor suspends himself and his subordinates, and then sends a message to his superior supervisor, "I failed!"
- When a supervisor receives a "failed" message from a subordinate, it can respond as follows:
- Failure fails, that's it, it's not much of a relationship: keep the current state, resume the actor, continue working
- It didn't work, so redo it again: Reset the status, restart the actor
- No chance, you lose, you go! Dismiss subordinate: Close end of the Actor
- I can not decide this matter, I ask my superiors: report to the superior actor
Some additional features of the Actor model implemented by 3.Akka
- When an actor is instantiated, it returns a Actorref, which is equivalent to an email address, and cannot get the status information of the actor through this
- The actor model is a higher-level abstraction of threads, and ultimately a thread that runs in Java.
- Multiple actors may share a thread, which is guaranteed by Akka
- Actor's mailbox comes in several ways: Unlimited variable mailbox with priority mailbox, and customizable implementation
- Akka didn't have the actor scan the mail message
- An actor end (whether normal or very) MSG in its mailbox enters the Akka system's "Dead Mailbox Dead Letter Mailbox"
Reference documents:
Http://alvinalexander.com/scala/simple-scala-akka-actor-examples-hello-world-actors
[Akka Basic Series 01] The Helloword of Akka