Glassfish gem - shared singleton

Hi Everyone,
I’m currently using the glassfish gem (0.9.1) with jruby (1.1.6) to host
a
rails application. I’m wondering if there is a recommended/supported
way of
having a singleton shared among multiple jruby runtimes. It appears
that
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?

Thanks,
Scott

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

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:

Thanks,
Scott


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

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

I should also mention additional benefits of running a single JRuby
instance:

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

Ikai

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

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

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

Ikai