如何在 Play Framework 2.4.2 scala 中正确安排任务?
How to correctly schedule task in Play Framework 2.4.2 scala?
尝试在 Play Framework 2.4.2 Scala 中安排这样的任务但运气不佳:
import akka.actor.Actor
import play.api.libs.concurrent.Akka
import scala.concurrent.duration._
import play.api.Play.current
import scala.concurrent.ExecutionContext.Implicits.global
class Scheduler extends Actor {
override def preStart() {
val dbupdate = Akka.system.scheduler.schedule(
0.microseconds, 5.minutes, self, "update")
val pictureClean = Akka.system.scheduler.schedule(
0.microseconds, 30.minutes, self, "clean")
}
def receive = {
case "update" => updateDB()
case "clean" => clean()
}
def updateDB(): Unit ={
Logger.debug("updates running")
}
def clean(): Unit ={
Logger.debug("cleanup running")
}
}
控制台中没有打印任何内容。我做错了什么?
尝试
context.system.scheduler.schedule
还要确保您的日志记录级别为 Debug
,否则这些消息将无法发送到控制台。如果您不确定,请尝试将它们暂时更改为 Logger.error
。
好的。这里是我构建的调度程序的工作代码:
模块:
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"
希望这对某人有所帮助
尝试在 Play Framework 2.4.2 Scala 中安排这样的任务但运气不佳:
import akka.actor.Actor
import play.api.libs.concurrent.Akka
import scala.concurrent.duration._
import play.api.Play.current
import scala.concurrent.ExecutionContext.Implicits.global
class Scheduler extends Actor {
override def preStart() {
val dbupdate = Akka.system.scheduler.schedule(
0.microseconds, 5.minutes, self, "update")
val pictureClean = Akka.system.scheduler.schedule(
0.microseconds, 30.minutes, self, "clean")
}
def receive = {
case "update" => updateDB()
case "clean" => clean()
}
def updateDB(): Unit ={
Logger.debug("updates running")
}
def clean(): Unit ={
Logger.debug("cleanup running")
}
}
控制台中没有打印任何内容。我做错了什么?
尝试
context.system.scheduler.schedule
还要确保您的日志记录级别为 Debug
,否则这些消息将无法发送到控制台。如果您不确定,请尝试将它们暂时更改为 Logger.error
。
好的。这里是我构建的调度程序的工作代码: 模块:
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"
希望这对某人有所帮助