scala – Play 2.4:使用依赖注入在app启动时安排定期任务
发布时间:2020-12-16 10:05:13 所属栏目:安全 来源:网络整理
导读:我需要在应用程序启动时安排一个重复的任务,任务本身非常简单,只需向应用程序发送一个即发即弃的HTTP调用.我不是游戏专家,我会认为直截了当的解决方案就像在Global.onStart中使用play.api.libs.concurrent.Akka.system.schedule.从Play 2.4开始,全局配置在某
我需要在应用程序启动时安排一个重复的任务,任务本身非常简单,只需向应用程序发送一个即发即弃的HTTP调用.我不是游戏专家,我会认为直截了当的解决方案就像在Global.onStart中使用play.api.libs.concurrent.Akka.system.schedule.从Play 2.4开始,全局配置在某种程度上已被弃用,有利于新的Guice DI.黑客从
DI documentation的建议我无法为这个问题找到一个很好的解决方案.我设法得到的最好的是在GuiceApplicationLoader之上编写一个包装器,调用BuiltInComponentsFromContext的自定义实现,但在这种情况下我不能使用注入来获取WSClient.使用Play 2.4重写类似内容的最佳方法是什么:
object Global extends GlobalSettings { override def onStart(app: Application) = { Akka.system.schedule(2.hours,2.hours,theTask) } } 解决方法
更新:现在可以更好地记录Play 2.6:
https://www.playframework.com/documentation/2.6.x/ScheduledTasks
您可以通过创建这样的模块来解决这个问题(注意代码注释): package tasks import javax.inject.{Singleton,Inject} import akka.actor.ActorSystem import com.google.inject.AbstractModule import play.api.inject.ApplicationLifecycle // Using the default ExecutionContext,but you can configure // your own as described here: // https://www.playframework.com/documentation/2.4.x/ThreadPools import play.api.libs.concurrent.Execution.Implicits.defaultContext import scala.concurrent.Future import scala.concurrent.duration._ class MyRecurrentTaskModule extends AbstractModule { override def configure() = { // binding the RecurrentTask as a eager singleton will force // its initialization even if RecurrentTask is not injected in // any other object. In other words,it will starts with when // your application starts. bind(classOf[RecurrentTask]).asEagerSingleton() } } @Singleton class RecurrentTask @Inject() (actorSystem: ActorSystem,lifecycle: ApplicationLifecycle) { // Just scheduling your task using the injected ActorSystem actorSystem.scheduler.schedule(1.second,1.second) { println("I'm running...") } // This is necessary to avoid thread leaks,specially if you are // using a custom ExecutionContext lifecycle.addStopHook{ () => Future.successful(actorSystem.shutdown()) } } 之后,您必须启用此模块,在conf / application.conf文件中添加以下行: play.modules.enabled += "tasks.MyRecurrentTaskModule" 然后,只需启动应用程序,向其发出请求,并查看计划任务将每秒运行一次. 参考文献: > Understanding Play thread pools 相关问题: > How to correctly schedule task in Play Framework 2.4.2 scala? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |