Doug Anderson
Doug Anderson

Reputation: 235

How do I access the actor system used in Play 2.5 from within a Module

I need access to the default actor system that Play Framework 2.5 uses from within my Module class.

I see that there is a method on ActorSystemProvider to get this:

@Singleton
class ActorSystemProvider @Inject()(environment: Environment, configuration: Configuration, applicationLifecycle: ApplicationLifecycle) extends Provider[ActorSystem] {
  private val logger = Logger(classOf[ActorSystemProvider])

  lazy val get: ActorSystem = {
    val (system, stopHook) = ActorSystemProvider.start(environment.classLoader, configuration)
    applicationLifecycle.addStopHook(stopHook)
    system
  }
}

But how do I get access to this class in my Module class?

For example:

class Module extends AbstractModule {
  val playSystem: ActorSytem = ???  
  ...
}

Upvotes: 0

Views: 933

Answers (1)

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

You can access actorSystem by simply injecting it into any of the component constructor. You will get access to the actorSystem created by play and you need not do any of the provider gymnastics.

For example, I need actor system to be accessible in my HomeController. So, I just inject into my HomeController constructor.

class HomeController @Inject() (actorSystem: ActorSystem) extends Controller {
  def index = Ok("bye!")
}

Upvotes: 1

Related Questions