How could(if there's a way) i start an context without being attached with the current thread? I mean, i'm actually making an integration with Akka, i have ways to garantee thread affinity with Akka's actors, but i would like to reduce thread numbers, but doing that i lose that thread affinity thus losing Rhino's context. Just for clarification, each actor will hold an context which will be responsible to answers requests which will run against Rhino's compiled code(which is compiled in preStart
due to code recycle).
Supposing i have an code like this:
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()
}
}
Edit:
Found a "way" storing context's factory and then factory.enterContext(scriptContext)
, but i don't think it's a good way, is it?