如果可能的话,我正在解决一个我真的想要解决的错误,而不是简单地找到解决办法。
这是我的代码:
import akka.actor._
import concurrent.duration._
sealed trait Message
case class ReturnInfluenceMessage(source: ActorRef) extends Message
case class SetInfluences(source: ActorRef) extends Message
case class Start(period:Int) extends Message
case object GetInfluence extends Message
case object Stop extends Message
case object TickMessage extends Message
class Listener extends Actor {
def receive = {
case _ =>
println("Listener: received something\n")
}
}
class Entity extends Actor {
val router = context.actorOf(Props[Router], name = "Router")
def receive = {
case rim @ ReturnInfluenceMessage(source) =>
source ! rim
case msg =>
shadow ! msg
}
}
class Router extends Actor {
val pulser = context.actorOf(Props[Pulser], name = "Pulser")
//more children
def receive = {
case rim @ ReturnInfluenceMessage(source) =>
context.parent ! rim
case msg =>
context.children foreach (_.forward(msg))
}
}
class Pulser extends Actor {
val clock = context.actorOf(Props[Clock], name = "Clock")
def receive = {
case TickMessage =>
println ("Emission_Unit: received Tickmessage!\n")
case _ =>
println ("Emission_Unit: received something\n")
}
}
class Clock extends Actor {
def receive = ready
def ready:Receive = {
case Start(period) => {
println ("Heartbeat: Start")
context.become(running(period) orElse ready)
context.system.scheduler.scheduleOnce(period milliseconds, self, TickMessage)
}
}
def running(period:Int):Receive = {
case TickMessage => {
context.parent ! TickMessage
context.system.scheduler.scheduleOnce(period milliseconds, self, TickMessage)
}
case Stop =>
println("Clock: stopping")
context.unbecome()
}
}
object Main extends App {
val system = akka.actor.ActorSystem("mySystem")
val abel = system.actorOf(Props[Listener], name = "Listener")
val cain = system.actorOf(Props[Entity], name = "Entity")
import system.dispatcher
system.scheduler.scheduleOnce(...)
//do something irrelevant
}
这不编译。似乎编译器无法识别行上的period
变量:
context.system.scheduler.scheduleOnce(period milliseconds, self, TickMessage)
这是错误:
[info] Compiling 1 Scala source to /home/yuggoth/witchdoctor/fff/target/scala-2.11/classes...
[error] /home/yuggoth/witchdoctor/fff/src/main/scala/prjt11.scala:64: Cannot find an implicit ExecutionContext. You might pass
[error] an (implicit ec: ExecutionContext) parameter to your method
[error] or import scala.concurrent.ExecutionContext.Implicits.global.
[error] context.system.scheduler.scheduleOnce(period milliseconds, self, TickMessage)
[error] ^
[error] /home/yuggoth/witchdoctor/fff/src/main/scala/prjt11.scala:71: Cannot find an implicit ExecutionContext. You might pass
[error] an (implicit ec: ExecutionContext) parameter to your method
[error] or import scala.concurrent.ExecutionContext.Implicits.global.
[error] context.system.scheduler.scheduleOnce(period milliseconds, self, TickMessage)
[error] ^
[error] two errors found
[error] (compile:compile) Compilation failed
[error] Total time: 4 s, completed Sep 2, 2014 4:46:52 PM
我在这里要做的就是让Clock
发送TickMessage
到parent Pulser
。我尝试使用我刚学到的状态设置而没有状态变量。
答案 0 :(得分:8)
尝试:
import scala.concurrent.ExecutionContext.Implicits.global
这对我有用
答案 1 :(得分:1)
编译器告诉您方法context.system.scheduler.scheduleOnce
需要隐式参数(implicit ec: ExecutionContext)
。
因此,您需要提供ExecutionContext
或导入可使implicit ExecutionContext
可见的范围。
快速查看akka doc告诉您使用:
import system.dispatcher
他们没有告诉你的是之前要定义system
,因为它不是一个包而是一个对象:
val system = akka.actor.ActorSystem("system")