3

我正在測試兩個Play之間Akka遠程演員的使用​​情況! v2.1-RC4應用程序。阿卡遠程演員之間無法通信播放!應用程序

下面是從應用1我的代碼

override def onStart(app: Application) { 

val system = ActorSystem("App1System", ConfigFactory.load.getConfig("app1")) 
val remoteActor = Akka.system().actorFor("akka://[email protected]: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") 
} 

這裏是應用1的配置文件:

從應用2
akka { 
    actor { 
    provider = "akka.remote.RemoteActorRefProvider" 
    } 
    remote { 
    transport = "akka.remote.netty.NettyRemoteTransport" 
    netty { 
     hostname = "127.0.0.1" 
     port = 9002 
    } 
    } 
} 

app1 { 
    include "common" 
} 

代碼:

從應用2
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}") 

    } 

配置:

akka { 
    actor { 
    provider = "akka.remote.RemoteActorRefProvider" 
    } 

    remote { 
    transport = "akka.remote.netty.NettyRemoteTransport" 
    netty { 
     hostname = "127.0.0.1" 
     port = 9003 
    } 
    } 
} 

app2 { 
    include "common" 
} 

我按照從鏈接中的說明,從應用1 Akka - Remoting

我的控制檯輸出:

[info] application - Application started 
[INFO] [02/06/2013 21:56:49.570] [New I/O worker #1] [NettyRemoteTransport(akka://[email protected]:9002)] [email protected]://[email protected]:9002 
[info] play - Starting application default Akka system. 
remote actor : akka://[email protected]: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://[email protected]:9002)] [email protected]://[email protected]: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://[email protected]:9002)] [email protected]://[email protected]:9003 
[INFO] [02/06/2013 21:57:10.635] [application-akka.actor.default-dispatcher-16] [NettyRemoteTransport(akka://[email protected]:9002)] [email protected]://[email protected]:9002 

我的控制檯輸出從應用2:

[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://[email protected]:9003)] [email protected]://[email protected]: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://[email protected]:9003)] [email protected]://[email protected]:9002 
[INFO] [02/06/2013 21:57:10.626] [application-7] [NettyRemoteTransport(akka://[email protected]:9003)] [email protected]://[email protected]:9002 

我可以看到,應用1能找到路徑上的App2 Actor(沒有DeadLetter),但是我沒有看到在App2控制檯上看到的消息。

不知道我在哪裏錯了。

回答

3

Akka.system是Play提供的演員系統,你把它和你自己的演員系統搞混了。儘量只使用自己的演員系統,即將代碼更改爲:

val remoteActor = system.actorFor 
val jobsActor = system.actorOf 
system.scheduler.schedule 

val app2Actor = system.actorOf 
+0

太棒了!我一回家就會測試。感謝Patrick。進行遠程處理時是否需要創建新的角色系統?無法播放默認演員系統使用? – cyberjar09

+1

是的,您應該可以使用Play的默認參與者系統,在這種情況下,您必須在application.conf中的「頂層」配置遠程處理(即在此設置akka.remote。*)。 –

+0

好吧,我做了一些改動並做了簡短的測試,但仍然沒有得到預期的結果,app2仍然沒有在控制檯上打印任何東西,我會在有更多時間調試後恢復。 – cyberjar09