Rhino:如何在没有线程关联的情况下创建上下文

Wes*_*óes 8 scala rhino akka

怎么可能(如果有办法)我启动上下文而不附加当前线程?我的意思是,我实际上正在与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()
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

找到了存储上下文工厂的"方式"然后factory.enterContext(scriptContext),但我不认为这是一个好方法,是吗?