在没有附加当前线程的情况下,如何启动上下文(如果可以的话)?我的意思是,我实际上是与Akka集成,我有办法保证与Akka演员的线程亲和性,但是我想减少线程数,但是这样做会失去线程亲和性,从而失去Rhino的上下文。为了澄清起见,每个参与者都将拥有一个上下文,该上下文负责回答将针对Rhino的已编译代码(由于代码回收而在preStart
中进行编译)运行的请求。
假设我有这样的代码:
class ScriptActor(script: String) extends Actor {
var scriptContext: Context = _
var scriptScope: Scriptable = _
override def receive: Receive = {
case ScriptActor.Run(env) =>
// 2: Here context's of current thread is asked with Context.getCurrentContext()
val func: RhinoFunction = scriptScope.get("$run", scriptScope)
.asInstanceOf[RhinoFunction]
val result = func.call(scriptContext, scriptScope, scriptScope, Array(env.noSpaces, signaler))
println(result)
}
override def postStop(): Unit = {
Context.exit()
super.postStop()
}
override def preStart(): Unit = {
// 1: Here context is bound to the actual thread
scriptContext = Context.enter()
scriptContext.setOptimizationLevel(-1)
scriptContext.setLanguageVersion(Context.VERSION_ES6)
scriptScope = scriptContext.initStandardObjects()
super.preStart()
}
}
编辑:
找到了一种存储上下文上下文工厂的“方式”,然后是factory.enterContext(scriptContext)
,但我认为这不是一个好方法,是吗?