今天偶然得机会,找到一篇不错得文章,现在分享给大家。

原文:http://www.kotancode.com/2013/02/15/managing-your-actor-systems/

If you’ve have any experience with Akka, then you know that the Actor System represents a hierarchy of managed actors. This hierarchy is a supervisoryhierarchy and allows for actor supervision much the way Erlang has supported process supervision for … I dunno, since the dinosaur age for sure.

Recently I’ve been writing some code for a non-blocking socket server that has all of its business logic encapsulated within an Akka Actor System. An actor system is a very heavy weight thing and most people recommend that you only keep one of these things around per process. You might feel tempted to create multiple systems to segment different purposes within your application but all of the information I’ve found discourages this practice. I haven’t had enough real-world experience to vouch for this but, people who are way smarter than I am say to only use one actor system per process, so that’s what I’ll do.

I had some trouble when it came time to reference my actor system. I had a companion object of an actor and, unfortunately, an actor’s companion object can’t see the context object that regular actor classes can see. In other words, my method couldn’t refer to the system via context.system, which is the recommended way for actors to refer to the system to which they belong.

So, I thought I would be clever and create a new instance of ActorSystem but re-use the same name that I used the last time. I thought maybe it would seek out and find the existing actor system and just create a new lightweight reference to it. This is wrong. Here’s the proof, create two vals:

1
2
val system = ActorSystem("foo")
val system2 = ActorSystem("foo")

Now, create an actor within system via system.actorOf and maintain a reference to it. If the two actor systems are identical, then you should be able to obtain a reference to that actor with system.actorFor using system2 and it’ll be the same actor. This is not the case. Even though both systems have the same name, they are different instances, managing different actor hierarchies, with different state and potentially divergent configurations.

My solution, which may not be the best, is to do the following, in order of precedence:

  1. Refer to the actor system within an Actor class via context.system. This won’t work within actor companion objects, however.
  2. Refer to the actor system by getting it from a global object, e.g. ApplicationServer.system. This should only be done when #1 won’t work.

If someone else has a more clever way of managing and referring to the actor system, I’d love to hear it. So far, this is the only way I’ve been able to keep my code anywhere close to clean.

Managing your Actor Systems的更多相关文章

  1. (转)Akka学习笔记(二):Actor Systems

    Akka学习笔记(二):Actor Systems 图中表示的是一个Actor System,它显示了在这个Actor System中最重要实体之间的关系. 什么是actor,是一个封装了状态和行为的 ...

  2. Akka - Basis for Distributed Computing

    Some concepts as blow: Welcome to Akka, a set of open-source libraries for designing scalable, resil ...

  3. Hadoop构成

    What Is Apache Hadoop? The Apache™ Hadoop® project develops open-source software for reliable, scala ...

  4. Welcome to Apache™ Hadoop®!

    What Is Apache Hadoop? Getting Started Download Hadoop Who Uses Hadoop? News 15 October, 2013: relea ...

  5. akka 文章 博客

    http://blog.csdn.net/wsscy2004/article/category/2430395 Actor生命周期理解 Actor生命周期理解 镇图:Actor内功心法图 Actor的 ...

  6. What Is Apache Hadoop

    What Is Apache Hadoop? The Apache™ Hadoop® project develops open-source software for reliable, scala ...

  7. Sr Software Engineer - Big Data Team

    Sr Software Engineer - Big Data Team   About UberWe’re changing the way people think about transport ...

  8. Security Software Engineer

    Security Software Engineer Are you excited to be part of the VR revolution and work on cutting edge ...

  9. akka模块

    模块 Akka的模块化做得非常好,它为不同的功能提供了不同的Jar包. akka-actor-2.0.jar – 标准Actor, 有类型Actor,等等 akka-remote-2.0.jar – ...

随机推荐

  1. Linux UDEV和为MySQL InnoDB共享表空间配置裸设备

    ⑴ UDEV 基础         udev 可管理保存在/dev 目录下的文件.文件只有在接入相应设备后才会生成.设备被拔出后自动删除     它还允许用户添加规则.以便修改/dev中默认的名称和权 ...

  2. windows 不能在 本地计算机 启动 Apache

    可能是Apache 的监听端口与其他软件有冲突,这是新手常犯的一个错误,Windows安装了IIS服务器的同时,又安装Apache服务器,二个服务器软件都监听TCP/IP协议的80端口,于是就有其中的 ...

  3. android动画效果演示

    第一种:TranslateAnimation  动画效果演示: public void move(View view) { // 传统动画效果 TranslateAnimation animation ...

  4. layout_weight属性详解

    看到上面这段代码,大家肯定认为三个TextView的宽度是1:2:2,但实际上是如图1这样的,宽度之比确实是1:2:2,但为什么第一个和后两个是齐平的呢?下面我给大家画一条线,可以看到虽然控件是没有对 ...

  5. E - Phone List(字典序,string类型使用)

    Description Given a list of phone numbers, determine if it is consistent in the sense that no number ...

  6. 字符设备驱动4: ioremap

    #define GPIO_OFT(x) ((x) - 0x56000000)#define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT ...

  7. 修改linux多系统启动顺序

    Ubuntu和XP双系统grub2默认启动项设置为XP 装了双系统后,在开机时总会有想让一个系统默认启动的时候,一般安装完Ubuntu和XP双系统后,开机时默认的是启动Ubuntu系统,但是当想让XP ...

  8. JavaScript弹出框

    confirm(str); 参数说明: str:在消息对话框中要显示的文本 返回值: Boolean值 返回值: 当用户点击"确定"按钮时,返回true 当用户点击"取消 ...

  9. Python数据结构之二叉树

    本来打算一个学期分别用C++.Python.Java实现数据结构,看来要提前了 这个是Python版本,我写的数据结构尽量保持灵活性,本文bt1是一般的插入法建立二叉树结构,bt2就是可以任意输入,至 ...

  10. oracle scn浅析

    1. 系统SCN号 查询系统SCN号的方法: select dbms_flashback.get_system_change_number from dual commit后系统SCN号会增长,但是即 ...