cyberjar09
cyberjar09

Reputation: 770

Akka remote actors cannot communicate between Play! applications

I am testing usage of Akka remote actors between two Play! v2.1-RC4 applications.

Here is my code from App1

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")
}

here is the config file of App1:

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

app1 {
  include "common"
}

Code from 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}")

  }

Config from App2 :

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

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

app2 {
  include "common"
}

I have followed the instructions from the link Akka - Remoting

My console output from App1 :

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

My console output from 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://[email protected]:9003)] RemoteServerStarted@akka://[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)] RemoteClientStarted@akka://[email protected]:9002
[INFO] [02/06/2013 21:57:10.626] [application-7] [NettyRemoteTransport(akka://[email protected]:9003)] RemoteClientShutdown@akka://[email protected]:9002

I can see that App1 is able to find the App2 Actor on the path (no DeadLetter), but none of the messages I expect to see on App2 console are appearing.

Not sure where I am going wrong here.

Upvotes: 3

Views: 1766

Answers (1)

Patrik Nordwall
Patrik Nordwall

Reputation: 2426

Akka.system is the actor system provided by Play, and you got that mixed up with your own actor systems. Try to only use your own actor systems, i.e. change the code to:

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

val app2Actor = system.actorOf

Upvotes: 3

Related Questions