我最近开始使用 Scala 和 Play Framework,并且刚刚将我一直在开发的服务升级到 Play 2.4.3。我的最终目标是创建一个夜间进程,在我的播放应用程序中启动一个服务方法,以便通过方法调用来调度事件,我当前正在使用 Actor 调用该方法。
我的基本想法是通过一个 Global.scala 文件来覆盖 onStart,但后来我看到了关于不再使用 GlobalSettings 的 play 文档(https://www.playframework.com/documentation/2.4.x ) /GlobalSettings)并一直试图将其转移到注入依赖方法。
这是我到目前为止所拼凑的内容:
模块代码:
import javax.inject._
import com.myOrganization.myPackage.Actors.ScheduleActor
import play.api.libs.concurrent.AkkaGuiceSupport
import play.libs.Akka
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import akka.actor.{ActorRef, ActorSystem}
import scala.concurrent.duration._
import play.Application
import com.google.inject.AbstractModule
@Singleton
class NightlyEvalSchedulerStartup @Inject()(system: ActorSystem, @Named("ScheduleActor") scheduleActor: ActorRef) {
Akka.system.scheduler.schedule(10.seconds, 20.seconds, scheduleActor, "ScheduleActor")
}
class ScheduleModule extends AbstractModule with AkkaGuiceSupport {
def configure() = {
bindActor[ScheduleActor]("ScheduleActor")
bind(classOf[NightlyEvalSchedulerStartup]).asEagerSingleton
}
}
Run Code Online (Sandbox Code Playgroud)
演员等级:
import akka.actor.{Actor, Props}
import com.myOrganization.myPackage.services.MySchedulingService
object ScheduleActor { …Run Code Online (Sandbox Code Playgroud)