Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use actors in play

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)
  }
}
like image 965
Georg Heiler Avatar asked Nov 25 '15 12:11

Georg Heiler


1 Answers

use

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

and import javax.inject.Singleton

like image 192
Johny T Koshy Avatar answered Oct 14 '22 14:10

Johny T Koshy