scala – 如何在Play 2.0中实现特定日期的预定工作?
发布时间:2020-12-16 18:58:48 所属栏目:安全 来源:网络整理
导读:Where is the job support in Play 2.0? 我已经阅读了这个主题,找到了使用Global和Akka间隔地执行计划作业的方法. 但是,在特定日期,仍然不了解计划的工作,例如,每天在午夜执行一次工作. Play 2.0不支持吗?如果没有,最好的方法是什么? 解决方法 您可以使用Q
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) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |