1. What's an Actor?
The Actor Model represents objects and their interactions, resembling
Human organizations and built upon the laws of physics.
is a object with identity
Has a behavior
Only interacts using asynchronous message passing
2. The Actor Trait
Type Receive = Partialfunction[any, Unit]
Trait Actor {
def receive:receive
...
}
The actor type describes the behavior of an actor, its response to
Messages.
3.A Simple Actor
Class Counter extends Actor {
var count = 0
def receive = {
Case "INCR" = + count + = 1
}
}
This object does not exhibit stateful behavior does not show any status of the actor
4. Making It Stateful let it be stateful
Actors can send messages to addresses (ACTORREF) They know (actor can send messages to the actor it knows):
Class Counter extends Actor {
var count = 0
def receive = {
Case "INCR" = + count + = 1
Case ("Get", customer:actorref) = customer! Count
}
}
5.
How Messages is Sent
Trait Actor {
Implicit Val Self:actorref
def sender:actorref
...
}
Abstract class Actorref {
Def! (Msg:any) (Implicit sender:actorref = Actor.nosender): Unit
def tell (Msg:any, sender:actorref) = this.! (msg) (sender)
...
}
Sending a message from one actor to the other picks up the sender ' s
Address implicitly. Sender's address is automatically passed to receiver
6. Send a message using the sender method
Using the Sender
Class Counter extends Actor {
var count = 0
def receive = {
Case "INCR" = + count + = 1
Case "GET" = sender! Count
}
}
7. The actor context defines the become and Unbecome methods that can change the actor's behavior
The Actor ' s Context
The Actor type describes the behavior, the execution is do by its
Actorcontext.
Trait Actorcontext {
def become (behavior:receive, Discardold:boolean = True): Unit
Def unbecome (): Unit
...
}
Trait Actor {
Implicit Val Context:actorcontext
...
}
8. Change actor's behavior
Changing an Actor ' s Behavior
Class Counter extends Actor {
def counter (n:int): Receive = {
Case "INCR" = context.become (Counter (n + 1))
Case "GET" = sender! N
}
def receive = counter (0)
}
9. Create actor and Storp methods using the Actorof method of the context stop actor
Creating and Stopping Actors
Trait Actorcontext {
def actorof (P:props, name:string): Actorref
def stop (a:actorref): Unit
...
}
Actors is created by Actors.
' Stop ' is often applied to ' self '.
10. Complete Example
Package Week5import Akka.actor.Actorimport Akka.actor.Propsclass Countermain extends actor {val counter = Context.actoro F (Props[counter], "Counter") Counter! "INCR" Counter! "INCR" Counter! "GET" def receive = {Case Count:int = println (S "Count is $count") Context.stop (self)}
Package Week5import Akka.actor.Actorclass Counter extends actor {var count = 0 def receive = {case "incr" = = Cou NT + = 1 case "Get" = sender! Count}}
Props[counter]-----Counter for the actor class defined below us
Counter----for the name of this new actor.
Once the actor receives the message, it can do the following
Upon reception of a message the actor can do any combination of the
Following:
Send messages Message
Create actors creating actor
Designate the behavior for the next message design the behavior logic after receiving the next messages
Akka 4 Akka Actor Model