7

編集通知、このhttps://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6の逆の変更を行って、受け入れられた回答をakkasホームページにある安定分布であるAKKA2.1で機能させる必要がありました。


AKKAで見つけたすべてのチュートリアルを読みましたが、「箱から出して」動作するものは見つかりませんでした。

Eclipseを使用して、2つのプログラムを作成したいと思います。

Program1:アクター「joe」を開始し、どういうわけか127.0.0.1:some_portで利用可能にします

Program2:127.0.0.1:some_portでアクター「joe」への参照を取得します。「joe」にハローメッセージを送信します。

プログラム1は、メッセージの受信時に何かを出力する必要があります。この例をAKKA2.1を使用してEclipseで実行したいと思います。誰かが2つのプログラム(program1とprogram2)を、これだけを実行する作業中のapplication.confファイルと一緒にリストすることはできますか?


編集> 私がこれまでに得たものをお見せしましょう:

俳優

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のみを起動すると機能し、次のように出力されます。

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/フォルダーと./フォルダーの両方に配置してみました。変わりはない。また、これは実際には降格展開であることを知っていますが、AKKAを使用して動作するにはHelloWorldプログラムが必要です。単純に機能するアプリケーションを入手せずに、これに多くの時間を費やしました。

4

4 に答える 4

19

Akka2.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という2つのファイルに配置する必要があります。どちらも、2つのプロジェクトのbinディレクトリ内にあります。

于 2013-03-12T17:13:16.247 に答える
6

korefnが述べたように、リモートドキュメントはそれが機能していることを詳細に説明しています。また、サンプルアプリケーションにリンクしています。その例はあなたが始めるために必要なすべてをあなたに与えるはずです。


編集

サンプルアプリケーションを実行するには、次の手順を実行します。

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
于 2013-02-18T13:33:26.000 に答える
0

さて、あなたの例では、クライアントコードは設定ファイルを参照することはなく、機能しません。

于 2013-10-24T13:37:56.063 に答える
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")
于 2016-06-29T15:16:04.780 に答える