注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

阿弥陀佛

街树飘影未见尘 潭月潜水了无声 般若观照心空静...

 
 
 

日志

 
 
关于我

一直从事气象预报、服务建模实践应用。 注重气象物理场、实况场、地理信息、本体知识库、分布式气象内容管理系统建立。 对Barnes客观分析, 小波,计算神经网络、信任传播、贝叶斯推理、专家系统、网络本体语言有一定体会。 一直使用Java、Delphi、Prolog、SQL编程。

网易考拉推荐

Sending objects as messages  

2014-09-18 15:46:10|  分类: Akka |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

A second Akka remote example: Sending objects as messages

By Alvin Alexander. Last updated: Jun 8, 2014

A few days ago I shared the source code for a simple Akka remote actor example. In that example I showed how to communicate between actors in two different JVMs using Scala and Akka. In that example I showed how to communicate between the two JVMs using de>Stringde> messages because I didn’t want to make the example any harder than necessary.

Today, I’m taking that example just one step further to show how to communicate between actors on different JVMs by using custom objects for your messages.

Discussion

I’m not going to show all the details of the project layout this time. For all those details, see my first example. I’ll just show a few things.

First, the main project contains three subdirectories:

  1. Common
  2. HelloLocal
  3. HelloRemote

As before, the HelloRemote directory contains the source code for the “remote” system, and HelloLocal contains the source code for the “local” actor system.

The Common project

In this project the Common directory contains the source code for the classes that are shared between the local and remote systems. That code is in the src/main/scala/common/Common.scala file, and looks like this:

de>packagede> de>commonde>
 
de>casede> de>objectde> de>Startde>
de>casede> de>classde> de>Message(msgde>de>:de> de>String)de>

The de>Startde> and de>Messagede> objects will be used to send messages between the local and remote systems. To do this, run the de>sbt packagede> command inside the Common directory. That command creates a JAR file named target/scala-2.10/common2.10-1.0.jar. Next, copy that JAR file to the lib_ folders in both the HelloLocal and HelloRemote projects.

The Remote project

The only thing new in the HelloRemote project compared to my first example is that it uses these objects rather than de>Stringde> instances to communicate with the HelloLocal project. The new code looks like this:

de>packagede> de>remotede>
 
de>importde> de>akka.actor.de>de>_de>
de>importde> de>common.de>de>_de>
 
de>objectde> de>HelloRemote de>de>extendsde> de>App  {de>
de>  de>de>valde> de>system de>de>=de> de>ActorSystem(de>de>"HelloRemoteSystem"de>de>)de>
de>  de>de>valde> de>remoteActor de>de>=de> de>system.actorOf(Props[RemoteActor], name de>de>=de> de>"RemoteActor"de>de>)de>
de>  de>de>remoteActor ! Message(de>de>"The RemoteActor is alive"de>de>)de>
de>}de>
 
de>classde> de>RemoteActor de>de>extendsde> de>Actor {de>
de>  de>de>defde> de>receive de>de>=de> de>{de>
de>    de>de>casede> de>Message(msg) de>de>=de>de>>de>
de>        de>de>println(sde>de>"RemoteActor received message '$msg'"de>de>)de>
de>        de>de>sender ! Message(de>de>"Hello from the RemoteActor"de>de>)de>
de>    de>de>casede> de>_de> de>=de>de>>de>
de>        de>de>println(de>de>"RemoteActor got something unexpected."de>de>)de>
 
de>  de>de>}de>
de>}de>

The Local project

Similarly, the only change to the HelloLocal project is that the code uses the custom objects as messages. The new code looks like this:

de>packagede> de>localde>
 
de>importde> de>akka.actor.de>de>_de>
de>importde> de>common.de>de>_de>
 
de>objectde> de>Local de>de>extendsde> de>App {de>
 
de>  de>de>implicitde> de>valde> de>system de>de>=de> de>ActorSystem(de>de>"LocalSystem"de>de>)de>
de>  de>de>valde> de>localActor de>de>=de> de>system.actorOf(Props[LocalActor], name de>de>=de> de>"LocalActor"de>de>)  de>de>// the local actorde>
de>  de>de>localActor ! Start                                                       de>de>// start the actionde>
 
de>}de>
 
de>classde> de>LocalActor de>de>extendsde> de>Actor {de>
 
de>  de>de>// create the remote actorde>
de>  de>de>valde> de>remote de>de>=de> de>context.actorFor(de>de>"akka://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor"de>de>)de>
de>  de>de>varde> de>counter de>de>=de> de>0de>
 
de>  de>de>defde> de>receive de>de>=de> de>{de>
de>    de>de>casede> de>Start de>de>=de>de>>de>
de>        de>de>remote ! Message(de>de>"Hello from the LocalActor"de>de>)de>
de>    de>de>casede> de>Message(msg) de>de>=de>de>>de>
de>        de>de>println(sde>de>"LocalActor received message: '$msg'"de>de>)de>
de>        de>de>ifde> de>(counter < de>de>5de>de>) {de>
de>            de>de>sender ! Message(de>de>"Hello back to you"de>de>)de>
de>            de>de>counter +de>de>=de> de>1de>
de>        de>de>}de>
de>    de>de>casede> de>_de> de>=de>de>>de>
de>        de>de>println(de>de>"LocalActor got something unexpected."de>de>)de>
de>  de>de>}de>
 
de>}de>
  评论这张
 
阅读(351)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017