how could I get instance of play's actor system in my custom application loader?

63 views
Skip to first unread message

ma...@knoldus.com

unread,
Mar 7, 2018, 4:20:33 PM3/7/18
to Play Framework
In Play 2.6, the default playframework application has an AsyncController. Ir needs the ActorSystem.

class AsyncController @Inject()(cc: ControllerComponents, actorSystem: ActorSystem)(implicit exec: ExecutionContext) extends AbstractController(cc) {

I am creating a code which needs compile time dependency injection. I suppose I have to create instance of all the controllers of my application and and pass it to Routes

lazy val router = new Routes(httpErrorHandler, homeController,userWSRoutes, countController,asyncController, assets)

My issue is that I dont know what to pass as ActorSystem while creating the instance of AsyncController.

lazy val asyncController = new controllers.AsyncController(controllerComponents, system) //what should be system.

I tried injecting (and also tried implicit) arguments but it didn't work. I could however create a new actor system and pass it to the controller but I dont want this. I want to get instance of the actor system which play creates by default ( I suppose it is called application)

lazy val asyncController = new controllers.AsyncController(controllerComponents, ActorSystem.create("CJ")) //this works but I am creating my own actor system. I need play's default system


Greg Methvin

unread,
Mar 7, 2018, 4:42:34 PM3/7/18
to play-framework
Usually if you use compile-time DI with play you will extend BuiltInComponentsFromContext. BuiltinComponents provides a lazy val actorSystem, which is the ActorSystem the application uses.

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/d7da6343-5b9e-4c08-856a-5c0c23203088%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Miguel Ángel Moreno

unread,
Mar 8, 2018, 5:32:28 PM3/8/18
to Play Framework
Hello

As Greg says, you have to mix BuiltInComponentsFromContext. In a project I am doing, I have something like the following code (using compile-time DI with macwire in my case)

class AppApplicationLoader extends ApplicationLoader {

  override def load(context: ApplicationLoader.Context): PlayApplication = {
    LoggerConfigurator(context.environment.classLoader).foreach { cfg =>
      cfg.configure(context.environment)
    }
    new AppComponents(context).application
  }
}

class AppComponents(context: Context) extends BuiltInComponentsFromContext(context) with MoreTraitsHere {

.... more stuff ...
  // Actors
  lazy val myActor: ActorRef = actorSystem.actorOf(MyActor.props(), MyActor.name)
  .... more stuff ...

}

inside application.conf
play.application.loader = "AppApplicationLoader"



As you can see, you just mix that trait and the actor system is available for you. Use it wisely, a great power comes with a great responsibility ;)

Miguel
Reply all
Reply to author
Forward
0 new messages