我在 Play Framework 2.5 中使用 akkaquartz ( https://github.com/theatrus/akka-quartz )制作了一个调度程序
一切都很好,直到我想取消作业,因为我无法访问保存 Cancellable 对象的变量。
下面是代码:
这是用于模块
RecurrentTaskModule.scala
class RecurrentTaskModule extends AbstractModule with AkkaGuiceSupport {
override def configure() = {
bindActor[WeatherHistoryApiActor]("weather-history-api-call")
bind(classOf[RecurrentTask]).asEagerSingleton()
}
}
@Singleton
class RecurrentTask @Inject() (actorSystem: ActorSystem,
@Named("weather-history-api-call") val weatherHistoryApiActor: ActorRef,
lifecycle: ApplicationLifecycle, scheduleModel: TxScheduleModel) {
var scheduleMap: Map[String, Cancellable] = _
val f = quartzActor ? AddCronSchedule(actorRef,
"%d %s %s %s %s %s".format(0,"*","*","*","*","*",
Message(""),true)
val r = Await.result(f, timeout.duration).asInstanceOf[AddCronScheduleResult]
r match {
case AddCronScheduleSuccess(cancel: Cancellable) => {
scheduleMap.put("%s".format(schedule._1), cancel)
}
case AddCronScheduleFailure(reason: Throwable) => {
}
}
}
在我的配置中
应用程序配置文件
play.modules.enabled += "tasks.RecurrentTaskModule"
如您所见,如果我可以访问
var scheduleMap
我可以通过调用地图中的可取消对象轻松取消作业
顺便说一句,我想在我的 Application.scala(控制器)中访问它
您可以将RecurrentTask
单身人士注入您的控制器实例:
class Application @Inject() (recurrentTask: RecurrentTask) {
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句