Where is the job support in Play 2.0?
我已经阅读了该线程,并找到了使用Global和Akka间隔来实现计划作业的方法。
但是对于在特定日期安排的作业仍然一无所知,例如,每天午夜执行一次的作业。
Play 2.0不支持吗?如果没有,最好的方法是什么?
最佳答案
您可以将Quartz库与CronTrigger结合使用,以在特定的日期/时间执行作业。看看他们的tutorial。这是一个带有简单调度程序的示例:
import java.util.Date
import org.quartz.JobBuilder.newJob
import org.quartz.SimpleScheduleBuilder.simpleSchedule
import org.quartz.TriggerBuilder.newTrigger
import org.quartz.impl.StdSchedulerFactory
import org.quartz.Job
import org.quartz.JobExecutionContext
import play.api.Application
import play.api.GlobalSettings
import play.api.Logger
object Global extends GlobalSettings {
val scheduler = StdSchedulerFactory.getDefaultScheduler();
override def onStart(app: Application) {
Logger.info("Quarz scheduler starting...")
scheduler.start();
// define the job and tie it to our HelloJob class
val job = newJob(classOf[MyWorker]).withIdentity("job1", "group1").build();
// Trigger the job to run now, and then repeat every 10 seconds
val trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(10)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
scheduler.scheduleJob(job, trigger);
}
override def onStop(app: Application) {
Logger.info("Quartz scheduler shutdown.")
scheduler.shutdown();
}
}
class MyWorker extends Job {
def execute(ctxt: JobExecutionContext) {
Logger.debug("Scheduled Job triggered at: " + new Date)
}
}
关于scala - 如何在Play 2.0中的特定日期实现排定的工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9883328/