Fel*_*lix 7 scala remote-actors actor akka
编辑注意,我需要对此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文件吗?
编辑> 让我告诉你到目前为止我得到了什么:
演员
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)
}
}
Run Code Online (Sandbox Code Playgroud)
计划1:
package test
import akka.actor.ActorSystem
object Machine1 {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MySystem")
}
}
Run Code Online (Sandbox Code Playgroud)
程序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")
}
}
Run Code Online (Sandbox Code Playgroud)
application.conf
akka {
actor {
deployment {
/greeter {
remote = "akka://MySystem@127.0.0.1:2553"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我只启动Program2并且输出时,此程序有效:
Hello Felix
[INFO] [02/18/2013 12:27:29.999] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/greeter] Hello Felix
Run Code Online (Sandbox Code Playgroud)
它似乎没有拿起我的application.conf.我尝试将它放在我的eclipse项目的./src/和./文件夹中.没有不同.另外,我知道这真的是降级部署,但我需要一个hello world程序才能使用AKKA.我花了很多时间在这上面而没有得到简单的工作应用程序.
Tho*_*ert 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")
}
Run Code Online (Sandbox Code Playgroud)
服务器的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
}
}
Run Code Online (Sandbox Code Playgroud)
客户:
客户端代码是
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")
}
Run Code Online (Sandbox Code Playgroud)
客户端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
}
}
}
Run Code Online (Sandbox Code Playgroud)
配置必须放在两个名为application.conf的文件中,这两个文件都位于两个项目的bin目录中.
正如korefn所提到的,远程文档详细解释了它的工作原理.它还链接到示例应用程序.该示例应该为您提供入门所需的一切.
要使示例应用程序运行,请执行以下步骤:
从GitHub克隆
eecolor@BLACK:~/GihHub$ git clone https://github.com/akka/akka.git
Run Code Online (Sandbox Code Playgroud)
进入akka
目录并运行sbt
eecolor@BLACK:~/GihHub/akka$ sbt
Run Code Online (Sandbox Code Playgroud)
切换到 akka-sample-project
akka > project akka-sample-remote
Run Code Online (Sandbox Code Playgroud)
调用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
Run Code Online (Sandbox Code Playgroud)
切换到另一个控制台并重复前几个步骤
eecolor@BLACK:~/GihHub/akka$ sbt
akka > project akka-sample-remote
Run Code Online (Sandbox Code Playgroud)
打电话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
Run Code Online (Sandbox Code Playgroud)
切换回另一个控制台,您应该看到如下内容:
Calculating 14 - 16
Calculating 13 - 22
Calculating 56 + 93
Calculating 18 + 19
Run Code Online (Sandbox Code Playgroud)