Akka使用系列之四:Future

这篇文章介绍 Akka 的同步机制,以及 Spark 和 Akka 的恩怨情仇。

专注于为中小企业提供成都网站制作、做网站、外贸营销网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业袁州免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

1. Akka 中的 Future

Akka 中的 Actor 发送和接收消息默认都是异步的。为了说明异步性,我们实行下面的数学老师和历史老师的 Actor:

 
 
 
 
  1. class MathTeacherActor extends Actor with ActorLogging { 
  2.     def receive = { 
  3.         case "1+1等于多少?"           => { 
  4.         Thread.sleep(1) 
  5.         sender ! "1+1等于2" 
  6.         } 
  7.     } 
  8. class HistoryTeacherActor extends Actor with ActorLogging { 
  9.     def receive = { 
  10.         case "历史上规模***的众筹行动是什么?" => { 
  11.             Thread.sleep(1) 
  12.             sender ! "历史上规模***的众筹行动是 +1s" 
  13.         } 
  14.     } 

如果我们在询问历史老师之后访问答案(如下面代码所示),我们发现并不能获取正确答案。原因就在于 Akka 是异步非阻塞的。

 
 
 
 
  1. val res = historyteacher ? "历史上规模***的众筹行动是什么?" 
  2. println(res) 

实质上, historyteacher ? "历史上规模***的众筹行动是什么?" 返回的根本不是答案,而是一个 Future。在Akka中, 一个Future是用来获取某个并发操作的结果的数据结构。有了 Future,我们可以以同步(阻塞)或异步(非阻塞)的方式访问结果。下面是简单地以同步(阻塞)方式访问结果的示例。

 
 
 
 
  1. class StudentActor(mathteacher:ActorRef,historyteacher:ActorRef) 
  2.  extends Actor with ActorLogging{ 
  3.   def receive = { 
  4.     case res:String => { 
  5.         val future1 = historyteacher ? "历史上规模***的众筹行动是什么?" 
  6.         val future2 = mathteacher ? "1+1等于多少?" 
  7.         val res1    = Await.result(future1,10 second) 
  8.         val res2    = Await.result(future2,10 second) 
  9.         println(res1) 
  10.         println(res2) 
  11.     } 
  12.  } 

2. Akka 和 Spark

Spark 一开始使用 Akka 作为内部通信部件。在 Spark 1.3 年代,为了解决大块数据(如Shuffle)的传输问题,Spark引入了Netty通信框架。到了 Spark 1.6, Spark 可以配置使用 Akka 或者 Netty 了,这意味着 Netty 可以完全替代 Akka 了。再到 Spark 2, Spark 已经完全抛弃 Akka 了,全部使用 Netty 了。Sad。

为什么 Spark 无情地有步骤有预谋地抛弃 Akka 呢?Spark 官方倒是给了一个说法:https://issues.apache.org/jira/browse/SPARK-5293。

A lot of Spark user applications are using (or want to use) Akka. Akka as a whole can contribute great architectural simplicity and uniformity. However, because Spark depends on Akka, it is not possible for users to rely on different versions, and we have received many requests in the past asking for help about this specific issue. For example, Spark Streaming might be used as the receiver of Akka messages - but our dependency on Akka requires the upstream Akka actors to also use the identical version of Akka.

Since our usage of Akka is limited (mainly for RPC and single-threaded event loop), we can replace it with alternative RPC implementations and a common event loop in Spark.

大意就是很多 Spark 用户希望同时使用 Spark 和 Akka ,但他们必须使用 Spark 依赖的那个版本的 Akka。Spark 主要用了 Akka 的 RPC 和 单线程 event-loop,因此 Spark 没有必要依赖完全的 Akka。最终,对 Akka 心心念念的 Spark 用 netty 实现下简易版本的 Akka。真爱啊。

3. 总结

到这里,Akka 使用系列就结束了。这个系列简单地过了一下 Akka 的基础知识,介绍其梗概。

【本文为专栏作者“李立”的原创稿件,转载请通过获取联系和授权】

分享题目:Akka使用系列之四:Future
文章路径:http://www.hantingmc.com/qtweb/news3/536753.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联