在游戏中使用演员

时间:2015-11-25 12:34:57

标签: playframework singleton akka playframework-2.4

我按照剧本描述如何使用演员:https://www.playframework.com/documentation/2.4.x/ScalaAkka他们建议如下:

@Singleton
class Application @Inject() (system: ActorSystem) extends Controller {

  val helloActor = system.actorOf(HelloActor.props, "hello-actor")

  //...
}

但结果是:

play.sbt.PlayExceptions$CompilationException: Compilation error[trait Singleton is abstract; cannot be instantiated]
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
        at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:27) ~[na:na]
        at scala.Option.map(Option.scala:145) ~[scala-library-2.11.6.jar:na]
        at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:49) ~[na:na]
        at play.sbt.run.PlayReload$$anonfun$taskFailureHandler$1.apply(PlayReload.scala:44) ~[na:na]
        at scala.Option.map(Option.scala:145) ~[scala-library-2.11.6.jar:na]
        at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:44) ~[na:na]
        at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:40) ~[na:na]
        at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]
        at play.sbt.run.PlayReload$$anonfun$compile$1.apply(PlayReload.scala:17) ~[na:na]

看看我做了什么或跟着做了什么:https://github.com/dataplayground/playground

编辑:

删除@Singleton会导致:

could not find implicit value for parameter timeout: akka.util.Timeout

这是代码:

implicit val timeout = 5.seconds

def sayHello(name: String) = Action.async {
  (helloActor ? SayHello(name)).mapTo[String].map { message =>
    Ok(message)
  }
}

1 个答案:

答案 0 :(得分:9)

使用

implicit val timeout = akka.util.Timeout(5.seconds)

import javax.inject.Singleton