Forum: JRuby Embedding API usage patterns and thread-safety

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Marko Asplund (Guest)
on 2012-07-23 22:32
(Received via mailing list)

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

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,

// invoke service
ruby.callMethod(service, "process_event", Object.class);

Does this look like a valid way to use the JRuby embedding API?


This topic is locked and can not be replied to.