[Akka Basic Series 01] The Helloword of Akka

Source: Internet
Author: User

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
    • 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:
      1. Failure fails, that's it, it's not much of a relationship: keep the current state, resume the actor, continue working
      2. It didn't work, so redo it again: Reset the status, restart the actor
      3. No chance, you lose, you go! Dismiss subordinate: Close end of the Actor
      4. 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:


[Akka Basic Series 01] The Helloword of 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.