Glassfish gem - shared singleton

Hi Everyone,
I’m currently using the glassfish gem (0.9.1) with jruby (1.1.6) to host
rails application. I’m wondering if there is a recommended/supported
way of
having a singleton shared among multiple jruby runtimes. It appears
each of the runtimes has its own child class loader (which makes sense)

  • so
    just naively requiring the jar file causes each runtime to have it’s own
    instance. Is there an easy way that I can get my singleton class loaded
    into the parent class loader before the runtimes get created?


Not really… However, there are a few hack-ish things that you could

Given that you are using gem, you might be able to modify the load
script to load your jar, where it should show up in the parent
classloader. I’m not sure if that would work at all, though.

You may also be able to do something Jave EE-ish and have a singleton
bean that you could access from all of the runtimes, but that would most
likely be painful and difficult.

Another possibility would be moving to threadsafe mode in Rails 2.2,
where there would always only be a single runtime, and you could have
your singleton easily (as long as you weren’t doing non-threadsafe
things with it, of course).

Scott Miller wrote:


To unsubscribe from this list, please visit:

Thanks for the response. That was going to be my follow up question:
confident are people with the new thread-safe mode? If it’s solid (in
2.2.2), then I’d prefer to take that approach anyway (and save some
as well).

I should also mention additional benefits of running a single JRuby

  • we cut our memory footprint in half on each app server
  • reduced startup time


Good to hear - this is definitely my preferred option.
Thanks much,

It’s solid. We’re running it on our production JRuby applications at

If you use MemCache, either enable threadsafe mode in memcache-client or
JRuby-MemCached wrapper
(GitHub - ikai/jruby-memcache-client: A wrapper of the Java Whalin MemCache client for JRuby).