我在两个Play之间测试Akka远程演员的用法! v2.1-RC4应用程序。
这是我的App1代码
override def onStart( app: Application ) {
val system = ActorSystem("App1System", ConfigFactory.load.getConfig("app1"))
val remoteActor = Akka.system().actorFor("akka://App2System@127.0.0.1:9003/user/app2Actor")
println(s"remote actor : ${remoteActor.path}")
val jobsActor = Akka.system.actorOf(Props(new Actor {
def receive = {
case "Job1" => { println("\nsending Job #1 at regular intervals to App2\n"); remoteActor ! "Job1" }
case "Job2" => { println("\n... sending doing job #2, 7 seconds after start, only once\n"); remoteActor ! "Job2" }
}
}), "app1Actor")
// Repeat every 5 seconds, start 5 seconds after start
Akka.system.scheduler.schedule(
5 seconds,
5 seconds,
jobsActor,
"Job1"
)
// do only once, 7 seconds after start
Akka.system.scheduler.scheduleOnce(7 seconds, jobsActor, "Job2")
}
这是App1的配置文件:
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "127.0.0.1"
port = 9002
}
}
}
app1 {
include "common"
}
App2代码:
override def onStart( app: Application ) {
val system = ActorSystem("App2System", ConfigFactory.load.getConfig("app2"))
val app2Actor = Akka.system.actorOf(Props(new Actor {
def receive = {
case "Job1" => println("App 2: doing Job #1 at regular intervals")
case "Job2" => println("App 2: ... doing job #2, 7 seconds after start, only once")
case _ => println("App 2 recieved other message")
}
}), "app2Actor")
println(s"app 2 actor : ${app2Actor.path}")
}
来自App2的配置:
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "127.0.0.1"
port = 9003
}
}
}
app2 {
include "common"
}
我已按照链接Akka - Remoting
中的说明操作App1的我的控制台输出:
[info] application - Application started
[INFO] [02/06/2013 21:56:49.570] [New I/O worker #1] [NettyRemoteTransport(akka://application@127.0.0.1:9002)] RemoteServerStarted@akka://application@127.0.0.1:9002
[info] play - Starting application default Akka system.
remote actor : akka://App2System@127.0.0.1:9003/user/app2Actor
[info] play - Application started (Dev)
sending Job #1 at regular intervals to App2
[INFO] [02/06/2013 21:56:54.918] [application-akka.actor.default-dispatcher-5] [NettyRemoteTransport(akka://application@127.0.0.1:9002)] RemoteClientStarted@akka://App2System@127.0.0.1:9003
... sending doing job #2, 7 seconds after start, only once
sending Job #1 at regular intervalsto App2
sending Job #1 at regular intervalsto App2
sending Job #1 at regular intervalsto App2
[info] application - Application stopped
[info] play - Shutdown application default Akka system.
[INFO] [02/06/2013 21:57:10.619] [application-akka.actor.default-dispatcher-16] [NettyRemoteTransport(akka://application@127.0.0.1:9002)] RemoteClientShutdown@akka://App2System@127.0.0.1:9003
[INFO] [02/06/2013 21:57:10.635] [application-akka.actor.default-dispatcher-16] [NettyRemoteTransport(akka://application@127.0.0.1:9002)] RemoteServerShutdown@akka://application@127.0.0.1:9002
App2的我的控制台输出:
[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:9001
(Server started, use Ctrl+D to stop and go back to the console...)
[info] Compiling 6 Scala sources and 1 Java source to /home/rudy/development/projects/Actor/target/scala-2.10/classes...
[info] application - Actor Application started
[INFO] [02/06/2013 21:50:41.899] [New I/O worker #1] [NettyRemoteTransport(akka://application@127.0.0.1:9003)] RemoteServerStarted@akka://application@127.0.0.1:9003
[info] play - Starting application default Akka system.
app 2 actor : akka://application/user/app2Actor
[info] play - Application started (Dev)
[INFO] [02/06/2013 21:56:54.961] [application-10] [NettyRemoteTransport(akka://application@127.0.0.1:9003)] RemoteClientStarted@akka://application@127.0.0.1:9002
[INFO] [02/06/2013 21:57:10.626] [application-7] [NettyRemoteTransport(akka://application@127.0.0.1:9003)] RemoteClientShutdown@akka://application@127.0.0.1:9002
我可以看到App1能够在路径上找到App2 Actor(没有DeadLetter),但是我希望在App2控制台上看到的消息都没有出现。
不知道我在哪里错了。
答案 0 :(得分:3)
Akka.system
是Play提供的actor系统,你可以将它与你自己的actor系统混合在一起。尝试仅使用您自己的actor系统,即将代码更改为:
val remoteActor = system.actorFor
val jobsActor = system.actorOf
system.scheduler.schedule
val app2Actor = system.actorOf