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.

最新文章

  1. win7下利用ftp实现华为路由器的上传和下载
  2. Ubuntu 下安装QT
  3. app端微信支付(二) - 生成预付单
  4. Redis集群的部署
  5. [分享]IOS开发-简单实现搜索框显示历史记录的本地缓存及搜索历史每次只能获取到一个的解决方案
  6. js函数的调用问题
  7. UVa 297 (四分树 递归) Quadtrees
  8. 设置一个POJO的某个属性的默认值
  9. unity 打包 windows 运行 紫色 粉红色
  10. POJ3468--A Simple Problem with Integers--线段树/树状数组 改段求段
  11. 时序分解算法:STL
  12. shiro中 UnknownAccountException
  13. RunLoop总结:RunLoop的应用场景(四)
  14. Bugku 杂项 眼见非实
  15. vue-cil和webpack中本地静态图片的路径问题解决方案
  16. DDD模型领域WF到领域层(十五)
  17. gulp给文件加版本号
  18. 【Zookeeper】源码分析之请求处理链(四)之FinalRequestProcessor
  19. bootstrap-select 下拉多选组件
  20. perl代码调试

热门文章

  1. 解决dragsort鼠标拖动与onclick事件共存
  2. C#委托同步异步说明,并比较control调用Invoke和BeginInvoke的异同
  3. [转]B+Tree图解
  4. C# enum 枚举 反射
  5. Live 直播过程
  6. Mac php7本地安装mongodb扩展以适配使用mongo扩展的线上老代码
  7. 51nod1228 序列求和(伯努利数)
  8. 常用跨平台IDE如何添加main函数的参数并正确执行
  9. 使用vue-cli脚手架搭建项目,保存编译时出现的代码检查错误(ESLint)
  10. Qt 学习之路 2(66):访问网络(2)