我目前正在使用 Rhino 在一个安静的服务中评估 javascript 脚本。我希望有一个评估超时。我创建了一个模拟示例演员(使用 scala 2.10 akka 演员)。
case class Evaluate(expression: String)
class RhinoActor extends Actor {
override def preStart() = { println("Start context'"); super.preStart()}
def receive = {
case Evaluate(expression) ⇒ {
Thread.sleep(100)
sender ! "complete"
}
}
override def postStop() = { println("Stop context'"); super.postStop()}
}
现在我运行使用这个演员如下:
def run {
val t = System.currentTimeMillis()
val system = ActorSystem("MySystem")
val actor = system.actorOf(Props[RhinoActor])
implicit val timeout = Timeout(50 milliseconds)
val future = (actor ? Evaluate("10 + 50")).mapTo[String]
val result = Try(Await.result(future, Duration.Inf))
println(System.currentTimeMillis() - t)
println(result)
actor ! PoisonPill
system.shutdown()
}
在这样的可能同时请求的闭包中使用 ActorSystem 是否明智?
我应该将 ActorSystem 设为全局吗,在这种情况下可以吗?
有没有更合适的替代方法?
编辑:我想我需要直接使用期货,但我需要 preStart 和 postStop。目前正在调查。编辑:似乎你没有得到期货的那些钩子。