How to define onStart method in scala play framework 2.4

how can i define startup job in play framework 2.4 with scala? play framework GlobalSetting I have already:

class StartupConfigurationModule extends AbstractModule{

  override def configure(): Unit = {
    Akka.system.scheduler.schedule(Duration(0,duration.HOURS),Duration(24,duration.HOURS))(Id3Service.start())
    Akka.system.dispatcher
  }

}

Answers


You need to register this in the modules.enabled of your application (in application.conf).

It should schedule a call to start on the Id3Service after 0 hours and then every 24 hours.

The issue is that the module doesn't declare a dependency on a running application, or more interestingly on a started actorSystem. Guice can decide to start it before the app is initialized.

The follwing is one way to force the dependency on the initialized actorSystem (and reduce the footprint of your dependency)

import javax.inject.{ Singleton, Inject }

import akka.actor.ActorSystem
import com.google.inject.AbstractModule

import scala.concurrent.duration._

class StartupConfigurationModule extends AbstractModule {

  override def configure(): Unit = {
    bind(classOf[Schedule]).asEagerSingleton()
  }

}

@Singleton
class Schedule @Inject() (actorSystem: ActorSystem) {
  implicit val ec = actorSystem.dispatcher
  actorSystem.scheduler.schedule(Duration(0, HOURS), Duration(24, HOURS))(Id3Service.start())
}
object Id3Service {
  def start(): Unit = println("started")
}

Need Your Help

Unit testing scala actors

unit-testing scala actor

Anyone know of a good way to unit test Scala actors? In the general sense I have an actor that receives a message and will send out other messages in response. This is done on multiple threads, and...

jQuery: Trigger even when checkbox gets checked

javascript jquery html checkbox

I am making a web app. In one part, I have a checklist dynamically generated with javascript.