Akka 2.1 最小远程角色示例 [英] Akka 2.1 minimal remote actor example

查看:34
本文介绍了Akka 2.1 最小远程角色示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑注意,我需要对此https://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6 使接受的答案适用于 AKKA 2.1,这是 akkas 主页上的稳定发行版!

EDIT Notice, I needed to make the reverse changes of this https://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6 to make the accepted answer work with AKKA 2.1 which is the stable distribution found on akkas homepage!

我已经阅读了我可以在 AKKA 上找到的所有教程,但我没有发现任何开箱即用"的教程.

I have read all the tutorials I could find on AKKA, but nothing I found works "out of box".

使用eclipse,我想创建2个程序.

Using eclipse, I want to create 2 programs.

程序 1:启动演员joe"并以某种方式使其在 127.0.0.1:some_port

Program1: starts actor "joe" and somehow makes it available on 127.0.0.1:some_port

程序2:在 127.0.0.1:some_port 获取对演员joe"的引用.向joe"发送问候消息.

Program2: gets a reference to actor "joe" at 127.0.0.1:some_port. Sends a hello message to "joe".

程序 1 应该在收到消息时打印一些内容.我想使用 AKKA 2.1 在 Eclipse 中运行这个例子.有人可以列出 2 个程序(程序 1 和程序 2)以及执行此操作的有效 application.conf 文件吗?

Program 1 should print something when the message is received. I want to run this example in eclipse using AKKA 2.1. Can someone list 2 programs, (program1 and program2) together with a working application.conf file that does this and nothing else?

编辑>让我向你展示我目前得到的:

edit> let me show you what I got so far:

演员

case class Greeting(who: String) extends Serializable

class GreetingActor extends Actor with ActorLogging {
  def receive = {
    case Greeting(who) ⇒ println("Hello " + who);log.info("Hello " + who)
  }
}

程序 1:

package test

import akka.actor.ActorSystem

object Machine1 {

  def main(args: Array[String]): Unit = {
    val system = ActorSystem("MySystem")
  }

}

程序 2

package test

import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.actorRef2Scala

object Machine2 {
  def main(args: Array[String]): Unit = {
    val system = ActorSystem("MySystem")
    val greeter = system.actorOf(Props[GreetingActor], name = "greeter")
    greeter ! Greeting("Felix")
  }
}

application.conf

akka {
  actor {
    deployment {
      /greeter {
        remote = "akka://MySystem@127.0.0.1:2553"
      }
    }
  }
}

但是,当我只启动 Program2 时,这个程序可以工作,并且输出:

However, this program works when I start only Program2 and it outputs:

Hello Felix
[INFO] [02/18/2013 12:27:29.999] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/greeter] Hello Felix

它似乎没有拿起我的application.conf.我尝试将它放在我的 eclipse 项目的 ./src/和 ./文件夹中.没有不同.另外,我知道这确实是降级部署,但我只需要一个 hello world 程序即可使用 AKKA 工作.我花了很多时间在这上面却没有得到一个简单的工作应用程序.

It seems that it is not picking up my application.conf. I tried placing it both in the ./src/ and ./ folder of my eclipse project. No difference. Also, I know this is really demote deployment, but I need just a hello world program to work using AKKA. I spent so much time on this without getting a simple working application.

推荐答案

正如 korefn 提到的,远程文档 详细解释了它的工作原理.它还链接到示例应用程序.该示例应为您提供入门所需的一切.

As korefn mentioned, the remote documentation explains it's workings in detail. It also links to an example application. That example should give you everything you need to get started.

要运行示例应用程序,请执行以下步骤:

To get the sample application running perform the following steps:

从 GitHub 克隆

Clone from GitHub

eecolor@BLACK:~/GihHub$ git clone https://github.com/akka/akka.git

进入akka目录并运行sbt

eecolor@BLACK:~/GihHub/akka$ sbt

切换到akka-sample-project

akka > project akka-sample-remote

在项目上调用 run 并选择 CalcApp

Call run on the project and select the CalcApp

Multiple main classes detected, select one to run:

 [1] sample.remote.calculator.java.JCreationApp
 [2] sample.remote.calculator.LookupApp
 [3] sample.remote.calculator.CalcApp
 [4] sample.remote.calculator.java.JLookupApp
 [5] sample.remote.calculator.CreationApp
 [6] sample.remote.calculator.java.JCalcApp

Enter number: 3

[info] Running sample.remote.calculator.CalcApp 
[INFO] [02/19/2013 19:22:09.055] [run-main] [Remoting] Starting remoting
[INFO] [02/19/2013 19:22:09.230] [run-main] [Remoting] Remoting started; listening on addresses :[akka.tcp://CalculatorApplication@127.0.0.1:2552]
Started Calculator Application - waiting for messages

切换到另一个控制台并重复前几步

Switch to another console and repeat the first few steps

eecolor@BLACK:~/GihHub/akka$ sbt
akka > project akka-sample-remote

调用 run 并选择 LookupApp

akka-sample-remote > run

Multiple main classes detected, select one to run:

 [1] sample.remote.calculator.java.JCreationApp
 [2] sample.remote.calculator.LookupApp
 [3] sample.remote.calculator.CalcApp
 [4] sample.remote.calculator.java.JLookupApp
 [5] sample.remote.calculator.CreationApp
 [6] sample.remote.calculator.java.JCalcApp

Enter number: 2

[info] Running sample.remote.calculator.LookupApp 
[INFO] [02/19/2013 19:23:39.358] [run-main] [Remoting] Starting remoting
[INFO] [02/19/2013 19:23:39.564] [run-main] [Remoting] Remoting started; listening on addresses :[akka.tcp://LookupApplication@127.0.0.1:2553]
Started Lookup Application
Sub result: 14 - 16 = -2
Sub result: 13 - 22 = -9
Add result: 56 + 93 = 149
Add result: 18 + 19 = 37

切换回另一个控制台,您应该会看到如下内容:

Switch back to the other console and you should see something like this:

Calculating 14 - 16
Calculating 13 - 22
Calculating 56 + 93
Calculating 18 + 19

这篇关于Akka 2.1 最小远程角色示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆