3
votes

For learning purposes I'm trying to implement a simple play application that gets data from a remote actor. The code for the actor is as follows:

import akka.actor.{Props, ActorSystem, Actor}

class NumbersServer extends Actor {
  var number = 0
  protected def receive = {
    case 'next => {
      number += 1
      number
    }
    case 'reset => number = 0
    case 'exit => context.stop(self)
    case 'get => sender ! number
  }
}

object Server {
  def main(args: Array[String]) {
    val system = ActorSystem("ServerSystem")
    val server = system.actorOf(Props[NumbersServer], "server")
  }
}

I package it into a jar and start it from the command line. If I try to send messages to this actor from a Scala console opened from another window, all works fine. Now I want to get the actor from the Play framework. In the Application object I define the following method:

def numbers = Action {
  Ok(views.html.numbers(Client.actor.path.name))
}

Then in the models package I define the Client object:

object Client {
  import play.api.Play.current
  val actor = Akka.system.actorFor("akka://[email protected]:2552/user/server")
}

The numbers.html.scala file:

@(message: String)

@main("Header") {
    <h1>@message</h1>
}

So I expect that when I go to 127.0.0.1:9000/numbers, I'd get a page with the path to the server actor. Instead of this, I get <h1>deadLetters</h1>. What do I do wrong and how this should be done correctly?

1
Have you've enabled remoting on both ends?Viktor Klang
@ViktorKlang : I tried to enable remoting in play in a way analogous to plain akka, but there is no akka-remote package, so I just got java.lang.ClassNotFoundException: akka.remote.RemoteActorRefProvider and not sure what else to do.Oleg Kunov
Sorry, maybe I don't understand something. I read those manuals, found them very useful, and in fact remoting works just fine when I do it in a plain akka application. But how do I enable akka remoting under the play framework? Is it enough to add the akka-remote dependency to sbt configuration of a play app and write the same config file as in plain akka?Oleg Kunov
You'll need to ask the Play! team about that, they ship a reference.conf file with Play! 2: github.com/playframework/Play20/blob/master/framework/src/play/… I assume that such things are documented in the Play! 2 docs. :-)Viktor Klang

1 Answers

0
votes

Please follow the configuration given in

https://groups.google.com/forum/#!topic/akka-user/Vw-B8nQeagk

And also add akka-remote dependency

val appDependencies = Seq(
    "com.typesafe.akka" % "akka-remote" % "2.0.2"
)