I've issues with my ScriptExecutor, that's called by multiple threads. I don't want any interference between two scripts executing in two different threads (by interference I mean global variables at least)
@Service
public class ScriptExecutor
{
private ScriptEngine engine;
public ScriptExecutor()
{
System.setProperty("org.jruby.embed.localcontext.scope", "threadsafe");
engine = new ScriptEngineManager().getEngineByName("jruby");
engine.getContext().setWriter(new OutputStreamWriter(...));
engine.getContext().setErrorWriter(new OutputStreamWriter(...));
}
public Object executeScript(String path, Object args){
{
//... Load script from path
engine.eval(r);
return (Invocable) engine.invokeFunction("myFunction", args);
}
}
Is this implementation thread safe ? By that I mean :
- do global jruby variables declared in each thread will be available only in the said thread (that's what I understand from this page)
- should I call eval(e, context) on a new context on every call of executeScript ? I was thinking about something like this solution
- Aren't those two solutions similar ?