以下是在Akka中使用计划的基本示例:
import akka.pattern
import akka.util.Timeout
import scala.concurrent.Await
import akka.actor.Actor
import akka.actor.Props
import akka.actor.ActorSystem
import akka.pattern.ask
import scala.concurrent.duration
object Application extends App {
val supervisor = ActorSystem().actorOf(Props[Supervisor])
implicit val timeout = Timeout(10 seconds)
import system.dispatcher
supervisor.scheduler.scheduleOnce(120 seconds) {
val future = supervisor ? Supervisor.Start
val resultIdList = Await.result(future, timeout.duration).asInstanceOf[List[MyIdList]]
supervisor ! resultIdList
}
}
我真的很困惑Akka的文档。这里Having problems with Akka 2.1.2 Scheduler ('system' not recognized)被称为import system.dispatcher
不是包导入,而是其他东西。那是什么?
什么是system
?我必须用supervisor
替换它吗?即使我没有这样做并继续使用system
,我也会遇到几乎相同的错误:
//(using system)
value scheduler is not a member of akka.actor.ActorRef
not found: value system
//or (using supervisor)
not found: value system
not found: value system
答案 0 :(得分:2)
试试这个;)
val system = ActorSystem()
val supervisor = system.actorOf(Props[Supervisor])
答案 1 :(得分:1)
(作为答案发布,因为不适合作为评论)
马吕斯,你指的是另一个以这条线开头的问题:val system = akka.actor.ActorSystem("system")
这是import语句所指的标识符'system'。 这条线
import system.dispatcher
表示变量系统的调度程序成员在范围内可用(您可以使用名称'dispatcher'从该点引用'system.dispatcher')。这也意味着因为调度程序是隐式的,它现在可用于隐式解析。请注意,时间表的签名是
scheduleOnce(delay: FiniteDuration, runnable: Runnable)(implicit executor: ExecutionContext): Cancellable
因此它需要显式传递的ExecutionContext或隐式传递。通过使用import语句,您可以将调度程序(ExecutionContext)带入作用域,因此您不必手动提供它。