avy
avy

Reputation: 437

play framework 2.5 starts from which file

I hava an akka actor. i want to start this actor at the start-up of application once in the life cycle of system.

currently i have used it at renderLoginPage Controller :

def loginPage: Action[AnyContent] = Action.async {
implicit request =>
  scheduler.sendReminder(kSessionService,userService)
  Logger.debug("Redirecting renderHomePage")
}

Following is my code of Scheduler for actor :

    class Scheduler{
  val system = ActorSystem("system")
  def sendReminder(kSessionService: KSessionService, userService: UserService):Unit = {
    val reminder = system.actorOf(ReminderActor.props(kSessionService,userService), "reminder-actor")
    reminder ! ReminderActor.Tick
  }

}

Now Problem occurring with me is : when i am logging out from the application it again renders login page and try to create the actor with same name . So i am getting an exception :

[InvalidActorNameException: actor name [reminder-actor] is not unique!]

Where i should write the code for initiating the scheduler.

Upvotes: 0

Views: 51

Answers (1)

Javier Vargas
Javier Vargas

Reputation: 94

You could do it without specifying a actor name:

system.actorOf(ReminderActor.props(kSessionService,userService))

But depending on how you implemented your actor you could have a single actor injected in your Action and send the data with the Tick message.

reminder ! ReminderActor.Tick(kSessionService,userService)

Check eager bindings: https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection#Eager-bindings

I think you can do something like:

class Module(system: ActorSystem) extends AbstractModule {
  def configure() = {
     //Set your binding here
  }
}

Upvotes: 1

Related Questions