Akka 4 akka的Actor模型

1. What is an Actor?

The Actor Model represents objects and their interactions, resembling

human organizations and built upon the laws of physics.

is an 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 不展示任何状态的actor


4. Making it Stateful 让它有状态

Actors can send messages to addresses (ActorRef) they know(Actor可以给它知道的Actor发消息):

class Counter extends Actor {

  var count = 0

  def receive = {

    case ”incr” => count += 1

    case (”get”, customer: ActorRef) => customer ! count

  }

}

5.

How Messages are 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的地址会自动传递给receiver

6. 使用sender方法发message

Using the Sender

class Counter extends Actor {

  var count = 0

  def receive = {

    case ”incr” => count += 1

    case ”get” => sender ! count

  }

}

7. Actor上下文定义了become和unbecome方法,可以改变actor的行为

The Actor’s Context

The Actor type describes the behavior, the execution is done by its

ActorContext.

trait ActorContext {

  def become(behavior: Receive, discardOld: Boolean = true): Unit

  def unbecome(): Unit

...

}

trait Actor {

  implicit val context: ActorContext

...

}

8.改变Actor的行为

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.使用context 的 actorOf方法创建actor和storp方法停止Actor

Creating and Stopping Actors

trait ActorContext {

  def actorOf(p: Props, name: String): ActorRef

  def stop(a: ActorRef): Unit

...

}

Actors are created by actors.

“stop” is often applied to “self”.


10. 完整示例

package week5

import akka.actor.Actor
import akka.actor.Props

class CounterMain extends Actor {
  val counter = context.actorOf(Props[Counter], "counter")

  counter ! "incr"
  counter ! "incr"
  counter ! "get"

  def receive = {
    case count: Int =>
      println(s"count was $count")
      context.stop(self)
  }
}
package week5

import akka.actor.Actor

class Counter extends Actor {
  var count = 0
  def receive = {
    case "incr" => count += 1
    case "get"  => sender ! count
  }
}

Props[Counter]  ----- Counter为我们下面定义的Actor类

counter ----  为这个新的Actor的名字


11. Actor在收到消息之后,可以做下面这些事


Upon reception of a message the actor can do any combination of the

following:

send messages   发消息

create actors   创建Actor

designate the behavior for the next message  设计收到下一条消息后的行为逻辑

相关文章
相关标签/搜索