The Actor is the unit of execution in Akka. Actors are object-oriented in the sense that they encapsulate state and behavior, but they have much stronger isolation than regular objects in Java or Scala.

The Actor model prevents sharing state between Actors and the only way to observe another actor's state is by sending it a message asking for it.

Actor模型限制分Actor們之間的共享狀態, 唯一觀察其他actor的方法是對它發送一個asking訊息.

 

Actors are extremely lightweight, they are only constrained by memory of which they consume only a few hundred bytes each — this means you can easily create millions of concurrent Actors in a single application. Their strong isolation principles together with the event-driven model (that we will talk about later on) and location transparency makes it easy to solve hard concurrency and scalability problems in an intuitive way.

Actors是極度輕量的, 他們只消費幾百個字節在內存的限制當中- 那表示你可以在一個簡單的應用程序輕松地建立數以百萬個並發Actors. 他們強大的隔離原則, 再加上事件驅動模型以及位置透明度使它很輕松地以直覺的方式去處理硬並發與可擴展性問題.

You create an Actor in Java by defining a class that extendsUntypedActor and implement the onReceive method (in Scala you have to extend Actor trait and implement the receive method). It is in theonReceive method that you define the behavior; how the Actor should react to the different messages it receives. An Actor can have — and often has — state. Accessing or mutating the internal state of an Actor is fully thread safe since protected by the Actor model.

So, let's now create a Greeter Actor with a single variable greeting as its state, holding on to the latest defined greeting, and in its onReceivemethod let's add the behavior for how it should react upon receiving the WhoToGreet and the Greet messages.

Let's start by creating our Actor in Java (you can find the code in the HelloAkkaJava.java file):

// Java code

public static class Greeter extends UntypedActor {
String greeting = ""; public void onReceive(Object message) {
if (message instanceof WhoToGreet)
greeting = "hello, " + ((WhoToGreet) message).who; else if (message instanceof Greet)
getSender().tell(new Greeting(greeting), getSelf()); else unhandled(message);
}
}

Actors like this one are “untyped” in the sense that the type of message received is not restricted—it is Object as shown above. There are also typed actors, but we will not concern ourselves with those now, the normal actors are the untyped ones.

Don't worry about the getSender(), tell(..) and getSelf() API calls, we will get to that soon when we talk about sending and replying to messages.

Now let's implement it in Scala. As you can see, Scala's pattern matching features really simplify working with Actors, but apart from that it's pretty similar to the Java version (you can find the code in theHelloAkkaScala.scala file).

// Scala code

class Greeter extends Actor {
var greeting = "" def receive = {
case WhoToGreet(who) => greeting = s"hello, $who"
case Greet => sender ! Greeting(greeting)
}
}

You will notice one difference to the Java version: here we do not explicitly pass unhandled messages to the unhandled() method. This is not necessary since the behavior defined by the receive method is expressed as a so-called partial function, which means that all messages for which no matching case statement is written down will be recognized as being not handled and Akka will automatically pass them to the unhandled() method for you.

Another difference is that the trait from which the Scala actor inherits is just called Actor. This is the Scala API while UntypedActor is the Java API for the same kind of actor.

[Activator-HelloAkka] Define our Actors的更多相关文章

  1. Define Constraints That Are Minimal and Sufficient 设定不多不少的约束

    Define Constraints That Are Minimal and Sufficient 设定不多不少的约束   今天第二章第二节. 主管不在,然后暂时没什么任务,把第二节看了,然后整理一 ...

  2. [Activator-HelloAkka] Create our Actors

    So far we have defined our Actor and its messages. Now let's create an instance of this actor. In Ak ...

  3. [Activator- HelloAkka] Define our Messages

    An Actor does not have a public API in terms of methods that you can invoke. Instead its public API ...

  4. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  5. [翻译]AKKA笔记 - CHILD ACTORS与ACTORPATH -6

    原文:http://rerun.me/2014/10/21/akka-notes-child-actors-and-path/ Actor是完全的继承结构.你创建的任何Actor肯定都是一个其他Act ...

  6. C#-#define条件编译

    本文导读: C#的预处理器指令从来不会转化为可执行代码的命令,但是会影响编译过程的各个方面,常用的预处理器指令有#define.#undef.#if,#elif,#else和#endif等等,下面介绍 ...

  7. 关于#define for if(false);else for

    今日在看一个第三方代码时看到了#define for if(false);else for 这样的一种定义,不明白这样用法的目的,于是查了一下. 这是一个兼容vc6.0的用法,csdn上有这个问题的回 ...

  8. #define与typedef在重定义类型中的区别

    #define 为完全的替换 typedef 重新定一个数据类型 eg #define charp1 char* typedef char* charp2charp1 a,b; //a char* b ...

  9. #pragma once与#ifndef #define ...#endif的区别

    1. #pragma once用来防止某个头文件被多次include: #ifndef,#define,#endif用来防止某个宏被多次定义.   2. #pragma once是编译相关,就是说这个 ...

随机推荐

  1. IEnumerable、GetEnumerator、IEnumerator之间的关系。

    了解了这些也就明白了遍历的原理,晚安. using System; using System.Collections; public class Person { public Person(stri ...

  2. Window 7 Professional 多语言设置

    1. 正常情况下,WINDOW系统只提供企业和旗舰版的语言切换的界面设置,其他版本没有. 2. 首先下载语言包,然后解压待用. 3. 以管理员身份运行命令窗口,如下输入: 4. 上面完成后,下载 ht ...

  3. iOS11.0后APP的图标和启动图

    随着Xcode9的更新,APP的图标和启动图也发生了略微变化,下面介绍下图标和启动图的设置. 1.APP图标: 这些是系统默认你开发的项目支持iPad.Spotlight等,其实真正我们的项目只要支持 ...

  4. win7 64位环境下,为python2.7 安装pip

    第一步: 安装python并配置好环境变量 参见:http://blog.csdn.net/donggege214/article/details/52062855 第二步: 下载setuptools ...

  5. mysql主从复制数据库

    mysql主从复制相信已经用得很多了,但是由于工作原因一直没怎么用过.趁着这段时间相对空闲,也就自己实现一遍.尽管互联网上已有大把类似的文章,但是自身实现的仍然值得记录. 环境: 主服务器:cento ...

  6. Python之路Python全局变量与局部变量、函数多层嵌套、函数递归

    Python之路Python全局变量与局部变量.函数多层嵌套.函数递归 一.局部变量与全局变量 1.在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量.全局变量作用域是整个程序,局 ...

  7. SQL语句优化 -- 以Mysql为例

     本文参考下面的文章:    1: [真·干货]MySQL 索引及优化实战 2:  Mysql语句的执行过程 3:  sql优化的几种方法 我将  sql语句优化分为三个方面,(此处不包括 业务逻辑的 ...

  8. 谈谈easyui datagrid 的数据加载

    文章目录 1url方式加载数据 1.1调用方式 1.2相关方法 1.3二次加载问题 2加载本地数据方式 2.1调用方式 2.2如何分页 2.3加载中效果 2.4如何不统计总数 这篇文章只谈jQuery ...

  9. phpstorm利用database连接mysql数据库

    首先声明一点,database只能连接一个已存在的数据库,不能创建数据库 连接一个已存在的数据库步骤: 1,找到database:连续点击俩次shift,输入database就能找到了 2,点击绿色的 ...

  10. angluarJs与后台交互get

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...