[Activator-HelloAkka] Define our Actors

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,布布扣,bubuko.com

时间: 2024-08-04 02:16:41

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

#define的用法

#define N 100  ok#define N 100; error#define N = 100  error   int a[N] => int a[= 100] error#define pin int*   pin a,b; error(a为int*,b为int) 2. 特殊用法 #define BEGIN {#define END } int main BEGIN    printf("haha");END 定义一个循环#define LOOP for(;;) 重

C# Activator实例化类的一般步骤

2016-01-06 1.获得要创建实例的类的类名 var className = "(命名空间namespace).ClassName"; 2.得到当前类的类型 var classType = Type.GetType(className); 3.创建实例化类的参数数组 var args = new object[] { object1, object2,object 3...}; 4.使用Activator实例化类 var classInstance = Activator.Cre

类型别名(define与typedef)

#define NEW OLD //使用预处理器的方法,为OLD定义一个新名称NEW,使用define定义的类型别名,会在预处理的过程中对NEW进行“单纯”的替换,例如: #define N 3+2 int i = N * 2; //预处理后,将会变成 int i = 3 + 2 * 2; //i 的结果将会是7 typedef typeName aliasName; //使用关键字typedef来创建别名,typedef不会创建新的类型,而只是为已知类型创建一个新名称. 两者比较: typed

OC高效率52之多用类型常量,少用#define预处理指令

// //  ViewController.m //  OC高效率52之多用类型常量,少用#define预处理指令 /**  *     1. 不要用预处理定义常量.这样定义出来的常量不含类型信息,编译器只是会在编译前据此执行查找与替换操作.即时有人重新定义了常量值,编译器也不会产生警告信息,这将导致应用程序中得常量值不一致.        2.在实现文件中使用static const 来定义"只在编译单元内可见的常量".由于此类常量不在全局符号表中,所以无需为其名称加前缀.     

【Android-tips】 Unable to execute dex: Multiple dex files define 解决方法

唔,之前已经想过今后不动android,没想到还是因为比赛的原因重操旧业.android有很多问题是由于eclipse的不完善造成的,比如今天遇到的这个问题 Unable to execute dex: Multiple dex files define [2011-10-23 16:23:29 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/myapp/R$array; [2011-10-23 16:23:

c++ typedef和#define的作用范围

typedef: 如果放在所有函数之外,它的作用域就是从它定义开始直到文件尾: 如果放在某个函数内,定义域就是从定义开始直到该函数结尾: #define: 不管是在某个函数内,还是在所有函数之外,作用域都是从定义开始直到整个文件结尾. define在同一编译单元内部,就算在不同的命名空间内,其作用范围不变.也就是从定义处一直到文件介绍. 看下面这个例子: Main.cpp /** * @file Main.cpp * @author chenjiashou([email protected])

define与typedef

#define与typedef有相似之处,但二者有本质区别 1 #define INTEGER int 和 typedef int INTEGER; 程序中INTEGER都可当做int使用,前者是预处理的宏代换,将程序中所有INTEGER先替换为int再进行编译,并没有产生新的名字:而后者是为int取了一个新的别名. 1 typerdef struct 2 { 3 int num; 4 char count; 5 }STUDENT;//STUDENT 是类型别名,注意它与直接定义结构体变量的区别

4、多用类型常量,少用#define预处理指令

摒弃: #define ANIMATION_DURATION 0.3 #define ERROR_MESSAGE @"ErrorMessage" 1)没有常量的类型信息 2)假设此指令声明在某个头文件中,那么所有引入了这个头文件的代码,都可以访问和修改ANIMATION_DURATION. 推荐: 1.私有常量 .m文件 static const NSTimeInterval kAnimationDuration = 0.3; static NSString *const kError

typedef与define宏定义用于声明新的类型之间的区别

摘自<c专家编程> typedef可以看成一种彻底的封装类型,在typedef声明类型之后不能再往里面增加其他的内容. 例子: #define peach int unsigned peach i; //没问题 typedef int banana; unsigned banana i; //错误,不能增加unsigned #define宏定义只是用于简单的替换 #define int_ptr int * int_ptr chalk, cheese; int * chalk, cheese;