Gerry
Gerry

Reputation: 5336

Akka actor construction in Play! framework

I am playing with the Play! framework and I wanted to create a factory called Services which will create new actors. I have this so far

class UserRegistration extends Actor {
  def receive = {
    case "foo" => "bar"
  }
}

object UserRegistration {
  val path: String = "user/registration"
}


object Services {
  val system = ActorSystem("Services")

  def apply[A<:Actor]: ActorRef = system.actorOf(Props[A], A.path)
}

And I wanted to create and pass messages to actors like this:

Services[UserRegistration] ? "hello"

but I get errors of the type could not find implicit value for evidence parameter of type ClassManifest[A]. Can anyone please tell me what am I doing wrong here? And if this is a valid construction in general (a best practice). I am pretty new in Scala and still learning stuff.

Thanks!

Upvotes: 1

Views: 601

Answers (2)

senia
senia

Reputation: 38045

Method apply of object Props implicitly takes parameter of type ClassManifest[T].

apply [T <: Actor] (implicit arg0: ClassManifest[T])

You have to add such parameter into your method:

def apply[A<:Actor : ClassManifest]: ActorRef = system.actorOf(Props[A])

Upvotes: 1

Kim Stebel
Kim Stebel

Reputation: 42047

senia's answer works too, but if you want to specify a common path for each type of actor, you need a second implicit parameter(the first being the ClassManifest so that you can specify a path for each type of actor. Keep in mind that actor names have to be unique though, so you have to add something to that path.

First you define a class that holds the Path:

case class Path(value:String)

Then you define an implicit value in the companion object of your actor class:

object SomeActor {
  implicit val path = Path("SomeActor")
}

Finally you modify the apply method to take an implicit class manifest as well as an implicit path.

def apply[A<:Actor](implicit cm:ClassManifest[A], path:Path[A]): ActorRef = 
  system.actorOf(Props[A], path.value + someIndex)

Upvotes: 1

Related Questions