编辑请注意,我需要对此https://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6进行反向更改,以使接受的答案适用于AKKA 2.1,这是在akkas主页上找到的稳定版本!
我已经阅读了我在AKKA上找到的所有教程,但我发现的任何内容都没有“开箱即用”。
使用eclipse,我想创建2个程序。
PROGRAM1: 开始演员“乔”并以某种方式使其在127.0.0.1:some_port
上可用Program2中: 在127.0.0.1:some_port获取对actor“joe”的引用。向“joe”发送一条问候消息。
程序1应该在收到消息时打印一些内容。我想在使用AKKA 2.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)
}
}
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。我没有得到简单的工作申请就花了很多时间。
答案 0 :(得分:18)
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目录中。
答案 1 :(得分:6)
正如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
答案 2 :(得分:0)
嗯,在您的示例中,客户端代码从不引用配置文件,它将无法正常工作。
答案 3 :(得分: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")