Forum: JRuby Embedding API usage patterns and thread-safety

C3a28fb3c8d3ff6a2acde13cce93ee01?d=identicon&s=25 Marko Asplund (Guest)
on 2012-07-23 22:32
(Received via mailing list)
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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.