我试图在 app/Global.scala 中创建一个全局对象来运行 Akka 调度程序,就像其他一些 SO 帖子所说的那样。
我的代码几乎是全局对象教程的复制/粘贴,但在启动时出现以下错误:
Oops, cannot start the server.
PlayException: Cannot init the Global object [Global cannot be cast to play.GlobalSettings]
at play.api.PlayException$.apply(Exceptions.scala:122)
at play.api.Application$$anonfun$3.apply(Application.scala:71)
at play.api.Application$$anonfun$3.apply(Application.scala:67)
at play.utils.Threads$.withContextClassLoader(Threads.scala:17)
at play.api.Application.<init>(Application.scala:66)
at play.core.StaticApplication.<init>(ApplicationProvider.scala:49)
at play.core.server.NettyServer$.createServer(NettyServer.scala:132)
at play.core.server.NettyServer$$anonfun$main$5.apply(NettyServer.scala:153)
at play.core.server.NettyServer$$anonfun$main$5.apply(NettyServer.scala:152)
at scala.Option.map(Option.scala:133)
at play.core.server.NettyServer$.main(NettyServer.scala:152)
at play.core.server.NettyServer.main(NettyServer.scala)
Caused by: java.lang.ClassCastException: Global cannot be cast to play.GlobalSettings
at play.api.Application.liftedTree1$1(Application.scala:44)
at play.api.Application.play$api$Application$$javaGlobal(Application.scala:43)
at play.api.Application$$anonfun$3.apply(Application.scala:68)
... 10 more
这是我的代码:
import play.api._
import play.api.Play.current
import akka.util.duration._
import play.api.libs.concurrent._
/**
* @author Sebastien Lorber (<i>lorber.sebastien@gmail.com</i>)
* Date: 21/11/12 - Time: 21:55
*/
class Global extends GlobalSettings {
override def onStart(app: Application) {
Logger.info("Application has started")
startElasticSearchIndexationScheduling
}
override def onStop(app: Application) {
Logger.info("Application shutdown...")
}
def startElasticSearchIndexationScheduling = {
Akka.system.scheduler.schedule(5 seconds, 15 seconds) {
Logger.info("Doing ElasticSearch reindexation (TODO: just testing scheduler")
}
}
}
没有你看到的那么花哨……
顺便说一句,我不明白为什么同名的对象经常有 2 个不同的包play.GlobalSettings
/play.api.GlobalSettings
等等。一个对象似乎用于 Java,另一个用于 Scala,但包名称使这一点不太明显。而且我正在使用 Scala 和导入“play.api.GlobalSettings”的 Scala 全局对象,那么为什么它需要尝试转换为play.GlobalSettings
看起来更面向 Java 的(我尝试扩展这个,但它也不起作用,它是不是教程所说的)
谢谢