Hi,
I’m working on an application that embeds JRuby in a multi-threaded
(servlet container) environment and I have a few questions regarding
thread-safety and JRuby embedding API usage patterns.
Here’re some code snippets and questions:
// Ruby rt + ScriptingContainer are initialized only once at startup
ScriptingContainer ruby = new
ScriptingContainer(LocalContextScope.CONCURRENT);
ruby.setLoadPaths(getRubyloadPaths());
Should a ScriptingContainer instance be shared between threads or
should one be instantiated for each thread (with a single shared Ruby
runtime being used behind the scenes)?
What happens if ScriptingContainer.setLoadPaths() is invoked after
other threads have started to use the ScriptingContainer (or the
shared Ruby runtime)?
When should Ruby libraries be loaded (require ‘foo’)? Can they be
loaded at any time? Will they be available to all threads? Are any
thread-safety issues involved?
// each request is processed by a different thread.
// a new Ruby service class instance is created to process each
request using the snipped below.
// create/convert input parameters
Object[] args = new Object[] {
ruby.runScriptlet(":symbolX"), obj1, obj2
};
// create service instance
Object srv = ruby.runScriptlet(“myRubyClassName”);
IRubyObject service = ruby.callMethod(srv, “new”, args,
IRubyObject.class);
// invoke service
ruby.callMethod(service, “process_event”, Object.class);
Does this look like a valid way to use the JRuby embedding API?
regards,
marko