如何在Play Framework 2.4.2 scala中正确安排任务?

时间:2015-07-28 14:40:01

标签: scala scheduled-tasks guice playframework-2.4

尝试在Play Framework 2.4.2 Scala中安排这样的任务,但没有运气:

http:\/\/.*?\/design\/view\/(.*?)

控制台中没有打印任何内容。我做错了什么?

2 个答案:

答案 0 :(得分:20)

确定。我在这里建立了调度程序的工作代码: 模块:

class JobModule extends AbstractModule with AkkaGuiceSupport {
  def configure() = {
    bindActor[SchedulerActor]("scheduler-actor")
    bind(classOf[Scheduler]).asEagerSingleton()
  }
}

调度程序:

class Scheduler @Inject() (val system: ActorSystem, @Named("scheduler-actor") val schedulerActor: ActorRef)(implicit ec: ExecutionContext)
{
  system.scheduler.schedule(
    0.microseconds, 5.minutes, schedulerActor, "update")
  system.scheduler.schedule(
    30.minutes, 30.days, schedulerActor, "clean")
}

演员:

@Singleton
class SchedulerActor @Inject() (updater: Updater) extends Actor {
  def receive = {
    case "update" => updateDB()
    case "clean" => clean()
  }

  def updateDB(): Unit ={
    Logger.debug("updates running")
  }

  def clean(): Unit ={
    Logger.debug("cleanup running")
  }
}

您还需要在application.conf中添加模块:

play.modules.enabled += "modules.JobModule"

希望这会有所帮助

答案 1 :(得分:0)

尝试

context.system.scheduler.schedule

并确保您已在Debug级别进行登录,否则这些消息将无法进入控制台。如果您不确定,请暂时将其更改为Logger.error