Pet*_*ter 8 scala startup scheduled-tasks playframework-2.0
我需要执行一个代码,允许在应用程序启动时启动预定作业,我该怎么做?谢谢.
Leo*_*Leo 15
使用Global对象 - 如果使用 - 必须在默认包中定义:
object Global extends play.api.GlobalSettings {
override def onStart(app: play.api.Application) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
请记住,在开发模式下,应用程序仅在第一个请求时加载,因此您必须触发启动该过程的请求.
自Play Framework 2.6x起
执行此操作的正确方法是使用具有急切绑定的自定义模块:
import scala.concurrent.Future
import javax.inject._
import play.api.inject.ApplicationLifecycle
// This creates an `ApplicationStart` object once at start-up and registers hook for shut-down.
@Singleton
class ApplicationStart @Inject() (lifecycle: ApplicationLifecycle) {
// Start up code here
// Shut-down hook
lifecycle.addStopHook { () =>
Future.successful(())
}
//...
}
Run Code Online (Sandbox Code Playgroud)
import com.google.inject.AbstractModule
class StartModule extends AbstractModule {
override def configure() = {
bind(classOf[ApplicationStart]).asEagerSingleton()
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅https://www.playframework.com/documentation/2.6.x/ScalaDependencyInjection#Eager-bindings