Use actors in play
Asked Answered
S

1

7

I follow along plays description how to use actors: https://www.playframework.com/documentation/2.4.x/ScalaAkka they suggest something like:

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

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

  //...
}

But this results in:

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]

Tos see what I did or follow along: https://github.com/dataplayground/playground

edit:

Removing the @Singleton leads to:

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

This is the code:

implicit val timeout = 5.seconds

def sayHello(name: String) = Action.async {
  (helloActor ? SayHello(name)).mapTo[String].map { message =>
    Ok(message)
  }
}
Stallfeed answered 25/11, 2015 at 12:34 Comment(5)
ok - so is the documentation outdated? at least this error goes away. However there still seems to be something wrong with the example. See editStallfeed
implicit val timeout = akka.util.TimeOut(5.seconds)Stripling
BTW, did you import javax.inject.Singleton?Stripling
Thank you very much! This is the solution. Should I write a solution? or would you like to write one?Stallfeed
NO I did not import the @Singleton as it was not mentioned here: playframework.com/documentation/2.4.x/ScalaAkkaStallfeed
S
9

use

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

and import javax.inject.Singleton

Stripling answered 25/11, 2015 at 12:44 Comment(1)
+ the original problem with @Singletonseems to be resolved by this fix. So it is possible to use it with imports of javax.inject.SingletonStallfeed

© 2022 - 2024 — McMap. All rights reserved.