Akka actor Getting Started example

Source: Internet
Author: User
What is Akka.
Akka usually refers to a distributed toolset, which is used to coordinate remote computing resources to do some work, and is an implementation of the actor concurrency model.


The origin of the actor.
A theoretical model of concurrent computing is proposed in this paper.


What is actor.
In the actor model, the actor is a concurrency primitive.


Actor and Message delivery
If multithreading reads and modifies the same value at the same time, it may be necessary to synchronize and lock.
Actors and objects differ in that they cannot be read, modified, or invoked directly. The actor can communicate with the outside world only by means of message delivery.
Message passing means that an actor can receive a message, or it can send a message, or respond to a received message.
Message passing is asynchronous. The actor has no reliance on the outside world, whether it is handling messages or replying to messages.
The actor processes only one message at a time. The essence of a mailbox is a queue of work waiting for the actor to process.


A few important concepts
Actor: A concurrent primitive that represents the work node, synchronously processing the received message. The actor can save and modify the internal state.
Message: Data that is used to communicate across processes, such as multiple actors.
Messaging: A software development paradigm that triggers various behaviors by passing messages, rather than triggering behavior directly.
e-mail address: The destination address of a message that is processed when the actor is idle and gets a message from that address.
Actor System: A collection of multiple actors and the email addresses, mailboxes, and configurations of these actors.


Actor's Benefits:
1. Each actor performs its own duties.
2. Eliminate shared status.
In multithreaded programming, a lock and synchronization mechanism is used to prevent multiple threads from reading and writing to the same value in order to ensure that the shared state value in memory does not appear to be wrong.
The actor solves this problem by reducing the sharing state.


Create the first actor
1. First construct the message
Creates an immutable message through the case class, and once the property's initial value is set in the constructor, only the property value can be read and no further modifications can be made.
Case Class Setrequest (key:string, Value:object)


2. Define the response after the actor receives the message
Now that the message has been defined, we can create the actor and describe how the actor received the message to react.
Import Akka.actor.Actor
Import akka.event.Logging
Import Scala.collection.mutable.HashMap


Class Akkademydb extends Actor {
Val map = new hashmap[string, Object]
Val log = Logging (Context.system, this)


Override Def receive = {
Case Setrequest (key, value) = {
Log.info ("Received Setrequest-key: {} value:", key, value)
Map.put (key, value)
}
Case o = Log.info ("Received unknown message: {}", O)
}
}


3. Interacting with the actor
Import Akka.actor.ActorSystem
Import Akka.testkit.TestActorRef
Import Akka.util.Timeout
Import Org.scalatest. {funspeclike, matchers}
Import Scala.concurrent.duration._


Class Akkademydbspec extends funspeclike with matchers {
Implicits val system = Actorsystem ()//Get Actor systems
Implicits val Timeout = timeout (5 seconds)


Describe ("Akkademydb") {
Describe ("given Setrequest") {
It ("should place key/value into map") {
Val actorref = testactorref (new Akkademydb)//create actor in actor system
Actorref! Setrequest ("Key", "value")
。 Passing messages into the actor's mailbox


Reference to actor instance behind
Val Akkademydb = Actorref.underlyingactor
Call Get ("key") to check the map to confirm that the value has been stored in map
AkkademyDb.map.get ("key") should equal (Some ("value"))
}
}
}
}

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.