Akka学习笔记:Actor消息传递(1)

10 篇文章 0 订阅

从前面的《Akka学习笔记:ACTORS介绍》,我们已经简单地了解了Actors。在这篇笔记中,我们将介绍Actor的消息传递。作为例子,我们将继续使用前面介绍的 Student-Teacher模型。
  在Actor消息传递的前部分,我们将创建Teacher Actor而不是Student Actor,我们将用一个称为StudentSimulatorApp的主程序。

详细回顾Student-Teacher模型

  我们现在考虑消息从StudentSimulatorApp单独发送到TeacherActor。这里所说的StudentSimulatorApp只不过是一个简单的主程序。


如果想及时了解 Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号: iteblog_hadoop

  这副图片解释如下:
  1、Student创建了一些东西,称为ActorSystem;
  2、他用ActorSystem来创建一个ActorRef,并将QuoteRequest message发送到ActorRef 中(到达TeacherActor的一个代理);
  3、ActorRef 将message单独传输到Dispatcher;
  4、Dispatcher将message按照顺序保存到目标Actor的MailBox中;
  5、然后Dispatcher将Mailbox放在一个Thread 中(更多详情将会在下节中进行介绍);
  6、MailBox按照队列顺序取出消息,并最终将它递给真实的TeacherActor接受方法中。
就像我所说的,不用担心。我们现在来一步一步地了解详情,当了解完详情之后,你可以返回来回顾上面六步。

StudentSimulatorApp程序

  我们将通过StudentSimulatorApp程序来启动JVM,并且初始化ActorSystem。


如果想及时了解 Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号: iteblog_hadoop

  正如我们从图中了解到,StudentSimulatorApp程序
  1、创建一个ActorSystem;
  2、用创建好的ActorSystem来创建一个通往Teacher Actor的代理 (ActorRef)
  3、将QuoteRequest message 发送到这个代理中。
  让我们单独探讨这三点。

1、创建一个ActorSystem

  ActorSystem是进入Actor世界的切入点,通过ActorSystem你可以创建和停止Actors,甚至关掉整个Actor环境!
  另一方面,Actor是一个体系,ActorSystem类似于java.lang.Object or scala.Any,能够容纳所有的Actor!它是所有的Actor的父类。当你创建一个Actor,你可以用ActorSystem的actorOf方法。


如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号: iteblog_hadoop

  初始化ActorSystem的代码类似下面:

1 val system=ActorSystem("UniversityMessageSystem")

UniversityMessageSystem就是你给ActorSystem取得名字。

2、为Teacher Actor创建代理

  让我们看下下面的代码片段:

1 val teacherActorRef:ActorRef=actorSystem.actorOf(Props[TeacherActor])

   actorOf是ActorSystem中创建Actor的方法。但是正如你所看到的,它并不返回我们所需要的TeacherActor对象,它的返回类型为ActorRef。
   ActorRef为真实Actor的充当代理,客户端并不直接和Actor进行通信。这就是Actor Model中的处理方式,该方式避免直接进入TeacherActor或者任何Actor中的任何custom/private方法或者变量。
  你仅仅将消息发送到ActorRef ,该消息最终发送到你的Actor中,你无法直接和你的Actor进行直接通信。如果你那样做,有人会恨你到死!!(这么严重?)


如果想及时了解Spark、Hadoop或者Hbase相关的文章,欢迎关注微信公共帐号: iteblog_hadoop

3、将QuoteRequest message 发送到代理中

   你仅仅告诉(tell)QuoteRequest message到ActorRef中,Actor中的tell方法是! 如下:

1 //send a message to the Teacher Actor
2   teacherActorRef!QuoteRequest

StudentSimulatorApp 的完整代码如下:

01 package me.rerun.akkanotes.messaging.actormsg1
02  
03 import akka.actor.ActorSystem 
04 import akka.actor.Props 
05 import akka.actor.actorRef2Scala 
06 import me.rerun.akkanotes.messaging.protocols.TeacherProtocol._
07  
08  
09 object StudentSimulatorApp extends App{
10  
11   //Initialize the ActorSystem
12   val actorSystem=ActorSystem("UniversityMessageSystem")
13  
14   //construct the Teacher Actor Ref
15   val teacherActorRef=actorSystem.actorOf(Props[TeacherActor])
16  
17   //send a message to the Teacher Actor
18   teacherActorRef!QuoteRequest
19  
20   //Let's wait for a couple of seconds before we shut down the system
21   Thread.sleep (2000)
22  
23   //Shut down the ActorSystem.
24   actorSystem.shutdown()
25  
26 }

   你需要调用shutdown方法来shutdownActorSystem ,否则,JVM会继续运行!我在程序中调用了 Thread.sleep (2000) 来休眠,这看起来很傻,但是你不用担心,在后面我将会创建许多的测试用例来避免这些。

   由于篇幅问题,剩下的内容在《Akka学习笔记:Actor消息传递(2)》里。
   本文翻译自:http://rerun.me/2014/09/19/akka-notes-actor-messaging-1/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值