首页 文章

Akka 2.1最小远程演员的例子

提问于
浏览
7

EDIT 注意,我需要对此https://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6进行相反的更改,以使接受的答案与AKKA 2.1一起使用,这是在akkas主页上找到的稳定分布!


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

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

Program1:启动actor“joe”并以某种方式使它在127.0.0.1:some_port上可用

Program2:在127.0.0.1:some_port获取对actor“joe”的引用 . 向“joe”发送hello消息 .

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


edit> 让我告诉你到目前为止我得到了什么:

actor

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)
  }
}

Program1

package test

import akka.actor.ActorSystem

object Machine1 {

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

}

Program2

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并输出时,此程序有效:

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 . 我花了这么多时间没有得到一个简单的工作应用程序 .

4 回答

  • 18

    正如korefn所提到的,remote documentation详细解释了它的工作原理 . 它还链接到example application . 该示例应该为您提供入门所需的一切 .


    编辑

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

    从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

    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
    

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

    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
    

    切换回另一个控制台,你应该看到这样的事情:

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

    Akka 2.2.3的更新

    可以按如下方式创建最小的远程应用程序:

    在Eclipse中创建2个项目:客户端和服务器

    服务器:

    服务器的代码是

    package server
    
    import akka.actor.Actor
    import akka.actor.ActorLogging
    import akka.actor.ActorSystem
    import akka.actor.Props
    
    class Joe extends Actor {
      def receive = {
        case msg: String => println("joe received " + msg + " from " + sender)
        case _ => println("Received unknown msg ")
      }
    }
    
    object Server extends App {
      val system = ActorSystem("GreetingSystem")
      val joe = system.actorOf(Props[Joe], name = "joe")
      println(joe.path)
      joe ! "local msg!"
      println("Server ready")
    }
    

    服务器的applincation.conf是

    akka {
      loglevel = "DEBUG"
      actor {
         provider = "akka.remote.RemoteActorRefProvider"
      }
      remote {
         enabled-transports = ["akka.remote.netty.tcp"]
         netty.tcp {
             hostname = "127.0.0.1"
             port = 2552
         }
         log-sent-messages = on
         log-received-messages = on
      }
    }
    

    客户:

    客户端代码是

    package client
    
    import akka.actor._
    import akka.actor.ActorDSL._
    
    object Greet_Sender extends App {
    
       println("STARTING")
    
       implicit val system = ActorSystem("GreetingSystem-1")
    
       val joe = system.actorSelection("akka.tcp://GreetingSystem@127.0.0.1:2552/user/joe")
    
       println("That 's Joe:" + joe)
    
       val a = actor(new Act {
          whenStarting { joe ! "Hello Joe from remote" }
       })
    
       joe ! "Hello"
    
       println("Client has sent Hello to joe")
    }
    

    客户端application.conf是:

    akka {
      #log-config-on-start = on
      stdout-loglevel = "DEBUG"
      loglevel = "DEBUG"
      actor {
          provider = "akka.remote.RemoteActorRefProvider"
      }
      remote {
        enabled-transports = ["akka.remote.netty.tcp"]
        log-sent-messages = on
        log-received-messages = on
        netty.tcp {
              hostname = "127.0.0.1"
              port = 0
        }
      }  
    }
    

    配置必须放在两个名为application.conf的文件中,这两个文件都位于两个项目的bin目录中 .

  • 6

    好吧,在您的示例中,客户端代码从不引用配置文件,它将无法正常工作 .

  • 0

    默认情况下,akka将使用application.conf文件 - 因此不需要显式选择它 .

    如果一个人想要那么代码将是(将上面的代码作为例子):

    val configFile = getClass.getClassLoader.getResource("akka_local_application.conf").getFile
    val config = ConfigFactory.parseFile(new File(configFile))
    val system = ActorSystem("GreetingSystem",config)
    val joe = system.actorOf(Props[Joe], name = "joe")
    

相关问题