Actor Path

我们知道actor是有层级的(hierarchical),第、每个actor在它的父actor的名字空间下都有一个名字。这样就构成了一个树状的结构,就像是文件系统。每个actor就像文件系统里的一个文件夹,因为每个actor都可以有子actor,因此,它们更像是文件夹,而不是文件。

val system = ActorSystem("HelloWorld")
val ref = system.actorOf(Props[Greeter], "greeter")
println(ref.path)
// prints: akka://HelloWorld/user/greeter

在这上面这段代码里,greeter的父actor是user(所有用户通过system.actorOf建的actor,都是user的子actor)。

greeter的path是akka://HelloWorld/user/greeter

这是一个URI。

其中authority part是akka://HelloWorld,这也是HelloWorld这个actor system的地址。akka://是一个协议,说明这是一个本地的actor path.

/user/greeter,是actor的path,这里就是greeter这个actor的path.

它们加起来就表示greeter这个actor的URI。

其实,每个actor system可以有多个地址,比如上边的HelloWorld当远程访问时,它的地址可以是 akka.tcp://HelloWorld@10.2.4.6:6565。其中akka.tcp说明这个这个actor system需要通过tcp协议访问,名字是HelloWorld, IP为10.2.4.6,端口为6565。如果访问这个actor system里的greeter这个actor,那么地址是akka.tcp://HelloWorld@10.2.4.6:6565/user/greeter

这说明每个actor system都可以多个地址,如果它能通过多个协议或者多个IP地址访问。

The difference between ActorPath and ActorRef

ActorPath和文件路径一样,你可以随便拼凑文件路径,即使它所指向的地方并没有文件。但是ActorRef一定指向一个存在的文件(对于actor,更像是文件夹,因为actor可以有子actor)。

每个ActorRef都会有一个path, 指向这个ActorRef。但是ActorPath可以指向一个并不存在的ActorRef,因此ActorPath是不可以被watch的,但是ActorRef可以。因为,ActorRef只能被以有限的方式获取,这些获取方式保证了它一定是某个已经在工作的actor的reference。无法获取一个并不存在的actor的ActorRef。 也因为如此,向一个ActorPath发送消息和向ActorRef发送消息是不同的。

另外,对于ActorRef,如果一个ActorPath指向的actor停止了,那么它的parent可以用同样的名字启动另一个actor,那么新的actor和已经终止的actor有同样的actorPath,但是它们有不同的ActorRef。ActorRef的最后有一个UID,它标识了ActorRef的不同。

Actor names are unique within a parent, but can be reused.

  • ActorPath is the full name, whether the actor exists or not.
  • ActorRef points to an actor which was started; an incarnation.

ActorPath can only optimistically send a message.

ActorRef can be watched.

ActorRef example: akka://HelloWorld/user/greeter#43438347   (print 一个ActorRef显示的值)

Resolving an ActorPath

case class Resolve(path: ActorPath)
case class Resolved(path: ActorPath, ref: ActorRef)
case class NotResoved(path: ActorPath)
case class ToBeResolved(path: ActorPath, client: ActorRef) class Resolver extends Actor{
def receive = {
case Resolve(path) => context.actorSelection(path) ! Identify(ToBeResolved(path, sender()))
case ActorIdentity(ToBeResolved(path, client), Some(actorRef)) => client ! Resolved(path, actorRef)
case ActorIdentity(ToBeResolved(path, client), None) => client ! NotResoved(path)
}
}

  有些时候我们不能直接获取ActorRef,比如另一个ActorSystem里,不是你创建的一个actor。这时候就需要另一套机制了,就是Identify和ActorIdentify。每一个Actor都能处理Identify消息,把自己的ActorRef封装于ActorIdentify中返回给sender。这就是通过通信来获取ActorRef, 总之, ActorRef是不能凭空先出来一个的,它一定代表一个存在的actor。

Relative Actor Paths

Looking up a grand-child:

  •  context.actorSelection("child/grandchild")

Looking up a sibling:

  • context.actorSelection("../sibling")

Looking up from the local root:

  • context.actorSelection("/user/app")

Broadcasting using wildcasts:

  • context.actorSelection("/user/controllers/*")

actor path的语义跟文件系统的path非常类似。

"Principles of Reactive Programming" 之<Actors are Distributed> (2)的更多相关文章

  1. "Principles of Reactive Programming" 之<Actors are Distributed> (1)

    week7中的前两节课的标题是”Actors are Distributed",讲了很多Akka Cluster的内容,同时也很难理解. Roland Kuhn并没有讲太多Akka Clus ...

  2. "Principles of Reactive Programming" 之<Actors are Distributed> (3)

    Cluster 讲课的这哥们接下来讲了下Akka Cluster的使用,但是是通过把一个以前讲过的actor 系统改成使用cluster来介绍的Akka cluster. 这部分代码很多,还是直接看视 ...

  3. "Principles of Reactive Programming" 之 <Persistent Actor State>学习笔记

    这是<Pinciples of Reactive Programming>week6的最后一课. 为什么需要把actor的状态持久化? 如果actor没有状态,那么在任何实时,这个acto ...

  4. Notes of Principles of Parallel Programming - TODO

    0.1 TopicNotes of Lin C., Snyder L.. Principles of Parallel Programming. Beijing: China Machine Pres ...

  5. "reactive programming"的概念

    下面的内容大多是翻译来的. Reactive Programming? What is Reactive Programming? 为了了解Reactive——从编程范式至其背后的动机,有必要了解现在 ...

  6. .Net中的反应式编程(Reactive Programming)

    系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程(Reactive programming)简称Rx,他是一个使用LI ...

  7. Unity基于响应式编程(Reactive programming)入门

    系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③—UGUI DoT ...

  8. ReactiveCocoa与Functional Reactive Programming

    转自 http://blog.leezhong.com/ios/2013/06/19/frp-reactivecocoa.html Functional Reactive Programming(以下 ...

  9. [Reactive Programming] RxJS dynamic behavior

    This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...

随机推荐

  1. PLSQL插入数据中文乱码的问题

    PLSQL插入数据中文乱码的问题 中文乱码就是编码不统一所导致的了,解决办法只需要把编码统一下即可解决了. 具体操作步骤如下: 1.查看服务器端编码 select userenv('language' ...

  2. Swift调用Objective-C

    Swift调用Objective-C需要一个名为“<工程名>-Bridging-Header.h”的桥接头文件,如下图所示.桥接头文件的作用是为Swift调用Objective-C对象搭建 ...

  3. KSImageNamed-Xcode插件在xcode 6.4/6.3或其他版本中不能使用解决方案

    大家都知道这个插件很强大,但是现在这个插件最新版貌似只支持xcode7 ,需要修改KSImageNamed-xcode中的一个配置文件,添加uuid才能使他支持xcode6.3或6.4 进入下载的插件 ...

  4. (转)mysql、sqlserver、oracle的默认事务的隔离级别

    1.mysql的默认事务的隔离级别:可重复读取(repeatable read); 2.sqlserver的默认事务的隔离级别:提交读取(read committed); 3.oracle的默认事务的 ...

  5. Java中的数组排序

    Java中的数组排序,一般是利用Arrays.sort(),这个方法是经过优化的快速排序.在Arrays种有多中形式的重载,在这里就不一一列举了. 数组排序的种类: 1.非降序排序, 非升序排序(就排 ...

  6. rabbitmq+haproxy+keepalived实现高可用集群搭建

    项目需要搭建rabbitmq的高可用集群,最近在学习搭建过程,在这里记录下可以跟大家一起互相交流(这里只是记录了学习之后自己的搭建过程,许多原理的东西没有细说). 搭建环境 CentOS7 64位 R ...

  7. 服务器 tfs不提供 TeamFoundation服务。基础连接已经关闭

    服务器 tfs(服务器名或url)不提供 TeamFoundation服务.基础连接已经关闭,发送时发生错误.TFS突然间连接不上到,到服务器上配置团队项目的组成员资格提示这样的错误,客户端连接的时候 ...

  8. WeX5是主要进行app开发吗?能开发微信App吗?

    WeX5是一款html5开发工具,可以进行app开发,做出各种H5 App,同样也可以进行主要运行在PC的html5产品,. WeX5开发的应用,不仅可以在微信上运行,也可以直接手机浏览器运行,或者打 ...

  9. 基于Golang的游戏服务器框架cellnet开发日记(二)

    看官们肯定还有大部分不是很熟悉Actor模型. 我这里基于Erlang, Skynet等语言和框架库来实战型解释下Actor模型.  Actor概念 Actor模型和OO类似, 都是符合人的思维模式进 ...

  10. PHP获取IP及地区信息(纯真IP数据库)

    昨天在写程序的时候,发现在用户的时候记录IP和地区信息也许以后用得上,去网上找了找,发现实现的方式有好多好多,因为我用的ThinkPHP,后来又去TP官网找了找,最后采用了下面这种方法. <?p ...