注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

阿弥陀佛

街树飘影未见尘 潭月潜水了无声 般若观照心空静...

 
 
 

日志

 
 
关于我

一直从事气象预报、服务建模实践应用。 注重气象物理场、实况场、地理信息、本体知识库、分布式气象内容管理系统建立。 对Barnes客观分析, 小波,计算神经网络、信任传播、贝叶斯推理、专家系统、网络本体语言有一定体会。 一直使用Java、Delphi、Prolog、SQL编程。

网易考拉推荐

Akka Actor System--Akka参与者系统  

2013-07-09 09:40:36|  分类: Akka |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Actor model      
 Actor是由状态(state)和行为(behavior)构成的对象(object)。Actor之间的排他性信息交换,通过邮箱实现。在某种意义上,Actor集合构成了一套严格的面向对象系统。以Actor集合为背景,给出抽象问题的解决方案模型,形成了Akka的Actor系统。可以想象为一组人完成一项任务,每个人承担其中的一项子任务,形成了具有一定组织结构、个人处理能力、相互交流的软件系统。
1. 阶层结构
       正如一个组织机构,Actor集合自然形成了一个阶层组织结构。一个Actor在阶层结构中仅仅负责完成一项确定子任务,具有一项功能。伴随任务的不断分解,Actor与任务的绑定,形成了Actor之间的阶层结构。上一级Actor,不仅完成属于自己的任务,还负责对下一级Actor的监管,即处理下一级Actor反馈的信息,发送指令给下一级Actor。阶层结构是一个递归结构,使得无论是处理成功的任务,或是处理失败的任务都能够在上一级监管Actor中得到反映。于是,Actor可分为:
        监管Actor。负责繁衍子Actor,并将任务分解到子Actor,检查子Actor的工作完成情况;
        误差内核(Error Kernel Pattern)模式Actor。负责找出、监管子Actor,并恰当处理孩子的错误;
        协同Actor。两个同级Actor完成任务的同时,需要关注对方的活性、终止提示。
2. 配置容器
        Actor系统是自然单元中的Actor协同集成,用于管理共享资源(例如:行程安排服务、配置、登录等)。几个不同配置的Actor系统可以共生于同一JVM,并不会发生冲突。在Akka中没有几个不同配置的Actor系统共享状态的概念。
3. 适用背景
    协同工作。
    无动态对象。
    由状态和行为构成的Actor。
    阶层结构。
4. Actor的构成
    Actor(State, Behavior,  Mailbox, Children, Supervisor Strategy, Actor Terminates)
5. 监管
    属下履历。保留属下累计的内部state
    从新启动属下。清除属下内部state
    终止属下。
    故障上报
---------------------------------------------------------------------------------------------------------------------------------------
1. 定义参与者
import akka.actor.Actor
import akka.actor.Props
import akka.event.Logging
class MyActor extends Actor {                                       //MyActor 总是从trait Actor派生
  val log = Logging(context.system, this)                       //receive 需要的偏函数(Partial Function)
  def receive = {                                                              //覆盖Actor中的receive偏函数方法
    case "test" ? log.info("received test")                         //一系列模式匹配(Pattern Match),确定参与者(MyActor)如何处理信息
    case _      ? log.info("received unknown message")  //对未知信息的处理
    //MyActor的返回值是Unit. The result of the receive method is a partial function object, which is stored within the actor as its “initial behavior”
  }
}                                                                              
-------------------------------------------------------------------------------------------------------------------------------------------
2. 创建支撑(Props)实例    
Props是MyActor的配置类。
import akka.actor.Props
val props1 = Props[MyActor]
val props3 = Props(classOf[MyActor], "arg")  //MyActor带构建参数的支撑
-----------------------------------------------------------------------------------------------------------------------------------------------
3. 创建带配置参数的参与者
object DemoActor {
  /**
   * Create Props for an actor of this type.
   * @param name The name to be passed to this actor’s constructor.
   * @return a Props for creating this actor, which can then be further configured
   *         (e.g. calling `.withDispatcher()` on it)
   */
  def props(name: String): Props = Props(classOf[DemoActor], name)
}

class DemoActor(name: String) extends Actor {
  def receive = {
    case x ? // some behavior
  }
}

// ...
context.actorOf(DemoActor.props("hello"))
-------------------------------------------------------------------------------------------------------------------------------------
4. 创建带支撑(Props)的MyActor
import akka.actor.ActorSystem
// ActorSystem is a heavy object: create only one per application
val system = ActorSystem("mySystem")
val myActor = system.actorOf(Props[MyActor], "myactor2") //actorOf是一个工厂方法。myActory位于system顶端top-level actors
记住:myActor supervised by the actor system’s provided guardian actor
-------------------------------------------------------------------------------------------
5.创建MyActor的儿子FirstActor
class FirstActor extends Actor { val child = context.actorOf(Props[MyActor], name = "myChild") // plus some behavior ... }
The call to actorOf returns an instance of ActorRef. The ActorRef is immutable and has a one to one relationship with the Actor it represents. The ActorRef is also serializable and network-aware. This means that you can serialize it, send it over the wire and use it on a remote host and it will still be representing the same Actor on the original node, across the network.
-----------------------------------------------------------------------------------------------------------------------------
6. Actor 应用程序接口(API)
The Actor trait defines only receive abstract method. If the current actor behavior does not match a received message, unhandled is called, which by default publishes an akka.actor.UnhandledMessage(message, sender, recipient) on the actor system’s event stream.

In addition, it offers:

  • self reference to the ActorRef of the actor

  • sender reference sender Actor of the last received message, typically used as described in Reply to messages

  • supervisorStrategy user overridable definition the strategy to use for supervising child actors

    This strategy is typically declared inside the actor in order to have access to the actor’s internal state within the decider function: since failure is communicated as a message sent to the supervisor and processed like other messages (albeit outside of the normal behavior), all values and variables within the actor are available, as is the sender reference (which will be the immediate child reporting the failure; if the original failure occurred within a distant descendant it is still reported one level up at a time).

  • context exposes contextual information for the actor and the current message, such as:

    • factory methods to create child actors (actorOf)
    • system that the actor belongs to
    • parent supervisor
    • supervised children
    • lifecycle monitoring
    • hotswap behavior stack
You can import the members in the context to avoid prefixing access with context.
class FirstActor extends Actor {
  import context._
  val myActor = actorOf(Props[MyActor], name = "myactor")
  def receive = {
    case x ? myActor ! x
  }
}
以下是
user-overridable方法,在Actor trait 默认为:
def preStart(): Unit = () def postStop(): Unit = () def preRestart(reason: Throwable, message: Option[Any]): Unit = { context.children foreach { child ? context.unwatch(child) context.stop(child) } postStop() } def postRestart(reason: Throwable): Unit = { preStart() }
7.参与者 Actory的生命循环(Lifecycal)
参与者来自于空(Empty path),在actorOf的作用下,开始了生命的旅程(path),UID是参与者的化身(incarnation)。。。生命终结后,UID被新的参与者占用,开始另一段新的生命的旅程(path)。
Akka Actor System--Akka参与者系统 - 险峰 - 阿弥陀佛
 8. 临终看护(DeathWatch)
import akka.actor.{ Actor, Props, Terminated }
class WatchActor extends Actor {
  val child = context.actorOf(Props.empty, "child")
  context.watch(child) // <-- this is the only call needed for registration
  var lastSender = system.deadLetters
  def receive = {
    case "kill" ?
      context.stop(child); lastSender = sender
    case Terminated(`child`) ? lastSender ! "finished"
  }
}
  评论这张
 
阅读(2322)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017