How do I pass java objects to an embedded JRuby container in a servlet enviornment?
I need a way to execute a jruby script in a multi-threaded environment, where each execution of the script is passed a different java object.
Currently, I am able to run self-contained scripts using code like:
public class Ex开发者_运维技巧ecutor {
public Executor() {
this.container = new ScriptingContainer(LocalContextScope.THREADSAFE, LocalVariableBehavior.TRANSIENT);
this.evalUnit = this.container.parse(getScriptContent());
}
public execute(HttpServletRequest request) {
this.evalUnit.run()
}
}
Which appears to perform well since the ruby script is parsed once in the constructor into the evalUnit and does not need to be re-parsed on each execution.
However, I want to be able to pass the request object to the script, but I cannot find the correct way to do that. There will be multiple simultaneous requests, so I don't think I can use this.container.put("$request", request), correct?
UPDATE In JRuby 1.6 there is now a LocalContextScope.CONCURRENT which appears to be what I am looking for. From what I can tell, if I construct ScriptingContainer as new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT) then I can call
container.getProvider().getVarMap().put("@request", request);
service.getEvalUnit().run();
and each thread will have its own @request value.
Am I understanding this usage correctly?
精彩评论