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. RabbitMQ单机集群搭建出现Error: unable to perform an operation on node 'rabbit1@ClusterNode1'

    参考链接:https://www.cnblogs.com/daryl/archive/2017/10/13/7645749.html 全部步骤和参考链接相同. 前八部都正常,在第九步会报错Error: ...

  2. angular启动过程

    第一步: .angular-cli.json 第二步: 第三步: 第四步: 第五步:

  3. 「POJ 2699」The Maximum Number of Strong Kings

    题目链接 戳我 \(Describe\) 一场联赛可以表示成一个完全图,点表示参赛选手,任意两点u, v之间有且仅有一条有向边\((u, v)\)或\((v, u)\),表示\(u\)打败\(v\)或 ...

  4. IO流-File,字节流,缓冲流

    1.1 IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再把 ...

  5. linux 虚拟机 安装 php-7.0.2

    1.安装依赖包 yum -y install libxml2 libxml2-devel openssl openssl-devel libjpeg libjpeg-devel libpng libp ...

  6. form表单提交回调函数

    form表单没有回调函数,不过可以通过jquery-form.js这个插件来实现回调函数: <form id="addform" class="form-horiz ...

  7. jdbc.properties文件的配置

    使用配置文件访问数据库的优点是: 一次编写随时调用,数据库类型发生变化只需要修改配置文件. 配置文件的设置: 在配置文件中,key-value对应的方式编写. 不好意思我只用过这两个数据库 :)--- ...

  8. 使用github和hexo搭建静态博客

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 终于写这篇文章了,这是我使用github和hexo搭建博客的一些心得,希望能给大家一点帮助.少走点弯路.刚接触github,只是用来存项目的版本, ...

  9. Python开发MapReduce系列(一)WordCount Demo

    原创,转发请注明出处. MapReduce是hadoop这只大象的核心,Hadoop 中,数据处理核心就是 MapReduce 程序设计模型.一个Map/Reduce 作业(job) 通常会把输入的数 ...

  10. STM32基础分析——PWM配置

    在使用STM32F103产生固定频率.固定占空比的PWM波时,虽然有官方以及众多开发板提供的例程,但是关于有点问题并没有说的很清晰,并且<STM32F10X参考手册>的中文翻译可能容易造成 ...