如何使用scala在play framework 2.4中定义启动作业? play framework GlobalSetting 我已经:
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
}
}
答案 0 :(得分:1)
您需要在应用的modules.enabled
中注册(application.conf
)。
它应安排在0小时后开始在Id3Service上启动,然后每24小时启动一次。
问题在于模块没有声明对正在运行的应用程序的依赖性,或者更有趣的是在已启动的actorSystem上。 Guice可以在应用初始化之前决定启动它。
以下是强制依赖初始化的actorSystem(并减少依赖关系的足迹)的一种方法
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")
}